compiler/libec/pass15: Fixed expType for class member of typed_object
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50
51       if(exp)
52          OutputExpression(exp, f);
53       f.Seek(0, start);
54       count = strlen(string);
55       count += f.Read(string + count, 1, 1023);
56       string[count] = '\0';
57       delete f;
58    }
59 }
60
61 Type ProcessTemplateParameterType(TemplateParameter param)
62 {
63    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
64    {
65       // TOFIX: Will need to free this Type
66       if(!param.baseType)
67       {
68          if(param.dataTypeString)
69             param.baseType = ProcessTypeString(param.dataTypeString, false);
70          else
71             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
72       }
73       return param.baseType;
74    }
75    return null;
76 }
77
78 bool NeedCast(Type type1, Type type2)
79 {
80    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
81
82    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
83    {
84       return false;
85    }
86
87    if(type1.kind == type2.kind)
88    {
89       switch(type1.kind)
90       {
91          case _BoolType:
92          case charType:
93          case shortType:
94          case intType:
95          case int64Type:
96          case intPtrType:
97          case intSizeType:
98             if(type1.passAsTemplate && !type2.passAsTemplate)
99                return true;
100             return type1.isSigned != type2.isSigned;
101          case classType:
102             return type1._class != type2._class;
103          case pointerType:
104             return NeedCast(type1.type, type2.type);
105          default:
106             return true; //false; ????
107       }
108    }
109    return true;
110 }
111
112 static void ReplaceClassMembers(Expression exp, Class _class)
113 {
114    if(exp.type == identifierExp && exp.identifier)
115    {
116       Identifier id = exp.identifier;
117       Context ctx;
118       Symbol symbol = null;
119       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
120       {
121          // First, check if the identifier is declared inside the function
122          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
123          {
124             symbol = (Symbol)ctx.symbols.FindString(id.string);
125             if(symbol) break;
126          }
127       }
128
129       // If it is not, check if it is a member of the _class
130       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
131       {
132          Property prop = eClass_FindProperty(_class, id.string, privateModule);
133          Method method = null;
134          DataMember member = null;
135          ClassProperty classProp = null;
136          if(!prop)
137          {
138             method = eClass_FindMethod(_class, id.string, privateModule);
139          }
140          if(!prop && !method)
141             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
142          if(!prop && !method && !member)
143          {
144             classProp = eClass_FindClassProperty(_class, id.string);
145          }
146          if(prop || method || member || classProp)
147          {
148             // Replace by this.[member]
149             exp.type = memberExp;
150             exp.member.member = id;
151             exp.member.memberType = unresolvedMember;
152             exp.member.exp = QMkExpId("this");
153             //exp.member.exp.loc = exp.loc;
154             exp.addedThis = true;
155          }
156          else if(_class && _class.templateParams.first)
157          {
158             Class sClass;
159             for(sClass = _class; sClass; sClass = sClass.base)
160             {
161                if(sClass.templateParams.first)
162                {
163                   ClassTemplateParameter param;
164                   for(param = sClass.templateParams.first; param; param = param.next)
165                   {
166                      if(param.type == expression && !strcmp(param.name, id.string))
167                      {
168                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
169
170                         if(argExp)
171                         {
172                            Declarator decl;
173                            OldList * specs = MkList();
174
175                            FreeIdentifier(exp.member.member);
176
177                            ProcessExpressionType(argExp);
178
179                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
180
181                            exp.expType = ProcessType(specs, decl);
182
183                            // *[expType] *[argExp]
184                            exp.type = bracketsExp;
185                            exp.list = MkListOne(MkExpOp(null, '*',
186                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
187                         }
188                      }
189                   }
190                }
191             }
192          }
193       }
194    }
195 }
196
197 ////////////////////////////////////////////////////////////////////////
198 // PRINTING ////////////////////////////////////////////////////////////
199 ////////////////////////////////////////////////////////////////////////
200
201 public char * PrintInt(int64 result)
202 {
203    char temp[100];
204    if(result > MAXINT64)
205       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
206    else
207       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
208    return CopyString(temp);
209 }
210
211 public char * PrintUInt(uint64 result)
212 {
213    char temp[100];
214    if(result > MAXDWORD)
215       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
216    else if(result > MAXINT)
217       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
218    else
219       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
220    return CopyString(temp);
221 }
222
223 public char * PrintInt64(int64 result)
224 {
225    char temp[100];
226    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
227    return CopyString(temp);
228 }
229
230 public char * PrintUInt64(uint64 result)
231 {
232    char temp[100];
233    if(result > MAXINT64)
234       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
235    else
236       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
237    return CopyString(temp);
238 }
239
240 public char * PrintHexUInt(uint64 result)
241 {
242    char temp[100];
243    if(result > MAXDWORD)
244       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
245    else
246       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
247    return CopyString(temp);
248 }
249
250 public char * PrintHexUInt64(uint64 result)
251 {
252    char temp[100];
253    if(result > MAXDWORD)
254       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
255    else
256       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
257    return CopyString(temp);
258 }
259
260 public char * PrintShort(short result)
261 {
262    char temp[100];
263    sprintf(temp, "%d", (unsigned short)result);
264    return CopyString(temp);
265 }
266
267 public char * PrintUShort(unsigned short result)
268 {
269    char temp[100];
270    if(result > 32767)
271       sprintf(temp, "0x%X", (int)result);
272    else
273       sprintf(temp, "%d", (int)result);
274    return CopyString(temp);
275 }
276
277 public char * PrintChar(char result)
278 {
279    char temp[100];
280    if(result > 0 && isprint(result))
281       sprintf(temp, "'%c'", result);
282    else if(result < 0)
283       sprintf(temp, "%d", (int)result);
284    else
285       //sprintf(temp, "%#X", result);
286       sprintf(temp, "0x%X", (unsigned char)result);
287    return CopyString(temp);
288 }
289
290 public char * PrintUChar(unsigned char result)
291 {
292    char temp[100];
293    sprintf(temp, "0x%X", result);
294    return CopyString(temp);
295 }
296
297 public char * PrintFloat(float result)
298 {
299    char temp[350];
300    sprintf(temp, "%.16ff", result);
301    return CopyString(temp);
302 }
303
304 public char * PrintDouble(double result)
305 {
306    char temp[350];
307    sprintf(temp, "%.16f", result);
308    return CopyString(temp);
309 }
310
311 ////////////////////////////////////////////////////////////////////////
312 ////////////////////////////////////////////////////////////////////////
313
314 //public Operand GetOperand(Expression exp);
315
316 #define GETVALUE(name, t) \
317    public bool Get##name(Expression exp, t * value2) \
318    {                                                        \
319       Operand op2 = GetOperand(exp);                        \
320       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
321       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
322       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
323       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
324       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
325       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
326       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
327       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
328       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
329       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
330       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
331       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
332       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
333       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
334       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
335       else                                                                          \
336          return false;                                                              \
337       return true;                                                                  \
338    }
339
340 // To help the deubugger currently not preprocessing...
341 #define HELP(x) x
342
343 GETVALUE(Int, HELP(int));
344 GETVALUE(UInt, HELP(unsigned int));
345 GETVALUE(Int64, HELP(int64));
346 GETVALUE(UInt64, HELP(uint64));
347 GETVALUE(IntPtr, HELP(intptr));
348 GETVALUE(UIntPtr, HELP(uintptr));
349 GETVALUE(IntSize, HELP(intsize));
350 GETVALUE(UIntSize, HELP(uintsize));
351 GETVALUE(Short, HELP(short));
352 GETVALUE(UShort, HELP(unsigned short));
353 GETVALUE(Char, HELP(char));
354 GETVALUE(UChar, HELP(unsigned char));
355 GETVALUE(Float, HELP(float));
356 GETVALUE(Double, HELP(double));
357
358 void ComputeExpression(Expression exp);
359
360 void ComputeClassMembers(Class _class, bool isMember)
361 {
362    DataMember member = isMember ? (DataMember) _class : null;
363    Context context = isMember ? null : SetupTemplatesContext(_class);
364    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
365                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
366    {
367       int c;
368       int unionMemberOffset = 0;
369       int bitFields = 0;
370
371       /*
372       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
373          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
374       */
375
376       if(member)
377       {
378          member.memberOffset = 0;
379          if(targetBits < sizeof(void *) * 8)
380             member.structAlignment = 0;
381       }
382       else if(targetBits < sizeof(void *) * 8)
383          _class.structAlignment = 0;
384
385       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
386       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
387          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
388
389       if(!member && _class.destructionWatchOffset)
390          _class.memberOffset += sizeof(OldList);
391
392       // To avoid reentrancy...
393       //_class.structSize = -1;
394
395       {
396          DataMember dataMember;
397          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
398          {
399             if(!dataMember.isProperty)
400             {
401                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
402                {
403                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
404                   /*if(!dataMember.dataType)
405                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
406                      */
407                }
408             }
409          }
410       }
411
412       {
413          DataMember dataMember;
414          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
415          {
416             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
417             {
418                if(!isMember && _class.type == bitClass && dataMember.dataType)
419                {
420                   BitMember bitMember = (BitMember) dataMember;
421                   uint64 mask = 0;
422                   int d;
423
424                   ComputeTypeSize(dataMember.dataType);
425
426                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
427                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
428
429                   _class.memberOffset = bitMember.pos + bitMember.size;
430                   for(d = 0; d<bitMember.size; d++)
431                   {
432                      if(d)
433                         mask <<= 1;
434                      mask |= 1;
435                   }
436                   bitMember.mask = mask << bitMember.pos;
437                }
438                else if(dataMember.type == normalMember && dataMember.dataType)
439                {
440                   int size;
441                   int alignment = 0;
442
443                   // Prevent infinite recursion
444                   if(dataMember.dataType.kind != classType ||
445                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
446                      _class.type != structClass)))
447                      ComputeTypeSize(dataMember.dataType);
448
449                   if(dataMember.dataType.bitFieldCount)
450                   {
451                      bitFields += dataMember.dataType.bitFieldCount;
452                      size = 0;
453                   }
454                   else
455                   {
456                      if(bitFields)
457                      {
458                         int size = (bitFields + 7) / 8;
459
460                         if(isMember)
461                         {
462                            // TESTING THIS PADDING CODE
463                            if(alignment)
464                            {
465                               member.structAlignment = Max(member.structAlignment, alignment);
466
467                               if(member.memberOffset % alignment)
468                                  member.memberOffset += alignment - (member.memberOffset % alignment);
469                            }
470
471                            dataMember.offset = member.memberOffset;
472                            if(member.type == unionMember)
473                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
474                            else
475                            {
476                               member.memberOffset += size;
477                            }
478                         }
479                         else
480                         {
481                            // TESTING THIS PADDING CODE
482                            if(alignment)
483                            {
484                               _class.structAlignment = Max(_class.structAlignment, alignment);
485
486                               if(_class.memberOffset % alignment)
487                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
488                            }
489
490                            dataMember.offset = _class.memberOffset;
491                            _class.memberOffset += size;
492                         }
493                         bitFields = 0;
494                      }
495                      size = dataMember.dataType.size;
496                      alignment = dataMember.dataType.alignment;
497                   }
498
499                   if(isMember)
500                   {
501                      // TESTING THIS PADDING CODE
502                      if(alignment)
503                      {
504                         member.structAlignment = Max(member.structAlignment, alignment);
505
506                         if(member.memberOffset % alignment)
507                            member.memberOffset += alignment - (member.memberOffset % alignment);
508                      }
509
510                      dataMember.offset = member.memberOffset;
511                      if(member.type == unionMember)
512                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
513                      else
514                      {
515                         member.memberOffset += size;
516                      }
517                   }
518                   else
519                   {
520                      // TESTING THIS PADDING CODE
521                      if(alignment)
522                      {
523                         _class.structAlignment = Max(_class.structAlignment, alignment);
524
525                         if(_class.memberOffset % alignment)
526                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
527                      }
528
529                      dataMember.offset = _class.memberOffset;
530                      _class.memberOffset += size;
531                   }
532                }
533                else
534                {
535                   int alignment;
536
537                   ComputeClassMembers((Class)dataMember, true);
538                   alignment = dataMember.structAlignment;
539
540                   if(isMember)
541                   {
542                      if(alignment)
543                      {
544                         if(member.memberOffset % alignment)
545                            member.memberOffset += alignment - (member.memberOffset % alignment);
546
547                         member.structAlignment = Max(member.structAlignment, alignment);
548                      }
549                      dataMember.offset = member.memberOffset;
550                      if(member.type == unionMember)
551                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
552                      else
553                         member.memberOffset += dataMember.memberOffset;
554                   }
555                   else
556                   {
557                      if(alignment)
558                      {
559                         if(_class.memberOffset % alignment)
560                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
561                         _class.structAlignment = Max(_class.structAlignment, alignment);
562                      }
563                      dataMember.offset = _class.memberOffset;
564                      _class.memberOffset += dataMember.memberOffset;
565                   }
566                }
567             }
568          }
569          if(bitFields)
570          {
571             int alignment = 0;
572             int size = (bitFields + 7) / 8;
573
574             if(isMember)
575             {
576                // TESTING THIS PADDING CODE
577                if(alignment)
578                {
579                   member.structAlignment = Max(member.structAlignment, alignment);
580
581                   if(member.memberOffset % alignment)
582                      member.memberOffset += alignment - (member.memberOffset % alignment);
583                }
584
585                if(member.type == unionMember)
586                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
587                else
588                {
589                   member.memberOffset += size;
590                }
591             }
592             else
593             {
594                // TESTING THIS PADDING CODE
595                if(alignment)
596                {
597                   _class.structAlignment = Max(_class.structAlignment, alignment);
598
599                   if(_class.memberOffset % alignment)
600                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
601                }
602                _class.memberOffset += size;
603             }
604             bitFields = 0;
605          }
606       }
607       if(member && member.type == unionMember)
608       {
609          member.memberOffset = unionMemberOffset;
610       }
611
612       if(!isMember)
613       {
614          /*if(_class.type == structClass)
615             _class.size = _class.memberOffset;
616          else
617          */
618
619          if(_class.type != bitClass)
620          {
621             int extra = 0;
622             if(_class.structAlignment)
623             {
624                if(_class.memberOffset % _class.structAlignment)
625                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
626             }
627             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
628             if(!member)
629             {
630                Property prop;
631                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
632                {
633                   if(prop.isProperty && prop.isWatchable)
634                   {
635                      prop.watcherOffset = _class.structSize;
636                      _class.structSize += sizeof(OldList);
637                   }
638                }
639             }
640
641             // Fix Derivatives
642             {
643                OldLink derivative;
644                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
645                {
646                   Class deriv = derivative.data;
647
648                   if(deriv.computeSize)
649                   {
650                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
651                      deriv.offset = /*_class.offset + */_class.structSize;
652                      deriv.memberOffset = 0;
653                      // ----------------------
654
655                      deriv.structSize = deriv.offset;
656
657                      ComputeClassMembers(deriv, false);
658                   }
659                }
660             }
661          }
662       }
663    }
664    if(context)
665       FinishTemplatesContext(context);
666 }
667
668 public void ComputeModuleClasses(Module module)
669 {
670    Class _class;
671    OldLink subModule;
672
673    for(subModule = module.modules.first; subModule; subModule = subModule.next)
674       ComputeModuleClasses(subModule.data);
675    for(_class = module.classes.first; _class; _class = _class.next)
676       ComputeClassMembers(_class, false);
677 }
678
679
680 public int ComputeTypeSize(Type type)
681 {
682    uint size = type ? type.size : 0;
683    if(!size && type && !type.computing)
684    {
685       type.computing = true;
686       switch(type.kind)
687       {
688          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
689          case charType: type.alignment = size = sizeof(char); break;
690          case intType: type.alignment = size = sizeof(int); break;
691          case int64Type: type.alignment = size = sizeof(int64); break;
692          case intPtrType: type.alignment = size = targetBits / 8; break;
693          case intSizeType: type.alignment = size = targetBits / 8; break;
694          case longType: type.alignment = size = sizeof(long); break;
695          case shortType: type.alignment = size = sizeof(short); break;
696          case floatType: type.alignment = size = sizeof(float); break;
697          case doubleType: type.alignment = size = sizeof(double); break;
698          case classType:
699          {
700             Class _class = type._class ? type._class.registered : null;
701
702             if(_class && _class.type == structClass)
703             {
704                // Ensure all members are properly registered
705                ComputeClassMembers(_class, false);
706                type.alignment = _class.structAlignment;
707                size = _class.structSize;
708                if(type.alignment && size % type.alignment)
709                   size += type.alignment - (size % type.alignment);
710
711             }
712             else if(_class && (_class.type == unitClass ||
713                    _class.type == enumClass ||
714                    _class.type == bitClass))
715             {
716                if(!_class.dataType)
717                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
718                size = type.alignment = ComputeTypeSize(_class.dataType);
719             }
720             else
721                size = type.alignment = targetBits / 8; // sizeof(Instance *);
722             break;
723          }
724          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
725          case arrayType:
726             if(type.arraySizeExp)
727             {
728                ProcessExpressionType(type.arraySizeExp);
729                ComputeExpression(type.arraySizeExp);
730                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
731                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
732                {
733                   Location oldLoc = yylloc;
734                   // bool isConstant = type.arraySizeExp.isConstant;
735                   char expression[10240];
736                   expression[0] = '\0';
737                   type.arraySizeExp.expType = null;
738                   yylloc = type.arraySizeExp.loc;
739                   if(inCompiler)
740                      PrintExpression(type.arraySizeExp, expression);
741                   Compiler_Error($"Array size not constant int (%s)\n", expression);
742                   yylloc = oldLoc;
743                }
744                GetInt(type.arraySizeExp, &type.arraySize);
745             }
746             else if(type.enumClass)
747             {
748                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
749                {
750                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
751                }
752                else
753                   type.arraySize = 0;
754             }
755             else
756             {
757                // Unimplemented auto size
758                type.arraySize = 0;
759             }
760
761             size = ComputeTypeSize(type.type) * type.arraySize;
762             if(type.type)
763                type.alignment = type.type.alignment;
764
765             break;
766          case structType:
767          {
768             Type member;
769             for(member = type.members.first; member; member = member.next)
770             {
771                uint addSize = ComputeTypeSize(member);
772
773                member.offset = size;
774                if(member.alignment && size % member.alignment)
775                   member.offset += member.alignment - (size % member.alignment);
776                size = member.offset;
777
778                type.alignment = Max(type.alignment, member.alignment);
779                size += addSize;
780             }
781             if(type.alignment && size % type.alignment)
782                size += type.alignment - (size % type.alignment);
783             break;
784          }
785          case unionType:
786          {
787             Type member;
788             for(member = type.members.first; member; member = member.next)
789             {
790                uint addSize = ComputeTypeSize(member);
791
792                member.offset = size;
793                if(member.alignment && size % member.alignment)
794                   member.offset += member.alignment - (size % member.alignment);
795                size = member.offset;
796
797                type.alignment = Max(type.alignment, member.alignment);
798                size = Max(size, addSize);
799             }
800             if(type.alignment && size % type.alignment)
801                size += type.alignment - (size % type.alignment);
802             break;
803          }
804          case templateType:
805          {
806             TemplateParameter param = type.templateParameter;
807             Type baseType = ProcessTemplateParameterType(param);
808             if(baseType)
809             {
810                size = ComputeTypeSize(baseType);
811                type.alignment = baseType.alignment;
812             }
813             else
814                type.alignment = size = sizeof(uint64);
815             break;
816          }
817          case enumType:
818          {
819             type.alignment = size = sizeof(enum { test });
820             break;
821          }
822          case thisClassType:
823          {
824             type.alignment = size = targetBits / 8; //sizeof(void *);
825             break;
826          }
827       }
828       type.size = size;
829       type.computing = false;
830    }
831    return size;
832 }
833
834
835 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
836 {
837    // This function is in need of a major review when implementing private members etc.
838    DataMember topMember = isMember ? (DataMember) _class : null;
839    uint totalSize = 0;
840    uint maxSize = 0;
841    int alignment, size;
842    DataMember member;
843    Context context = isMember ? null : SetupTemplatesContext(_class);
844    if(addedPadding)
845       *addedPadding = false;
846
847    if(!isMember && _class.base)
848    {
849       maxSize = _class.structSize;
850       //if(_class.base.type != systemClass) // Commented out with new Instance _class
851       {
852          // DANGER: Testing this noHeadClass here...
853          if(_class.type == structClass || _class.type == noHeadClass)
854             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
855          else
856          {
857             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
858             if(maxSize > baseSize)
859                maxSize -= baseSize;
860             else
861                maxSize = 0;
862          }
863       }
864    }
865
866    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
867    {
868       if(!member.isProperty)
869       {
870          switch(member.type)
871          {
872             case normalMember:
873             {
874                if(member.dataTypeString)
875                {
876                   OldList * specs = MkList(), * decls = MkList();
877                   Declarator decl;
878
879                   decl = SpecDeclFromString(member.dataTypeString, specs,
880                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
881                   ListAdd(decls, MkStructDeclarator(decl, null));
882                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
883
884                   if(!member.dataType)
885                      member.dataType = ProcessType(specs, decl);
886
887                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
888
889                   {
890                      Type type = ProcessType(specs, decl);
891                      DeclareType(member.dataType, false, false);
892                      FreeType(type);
893                   }
894                   /*
895                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
896                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
897                      DeclareStruct(member.dataType._class.string, false);
898                   */
899
900                   ComputeTypeSize(member.dataType);
901                   size = member.dataType.size;
902                   alignment = member.dataType.alignment;
903
904                   if(alignment)
905                   {
906                      if(totalSize % alignment)
907                         totalSize += alignment - (totalSize % alignment);
908                   }
909                   totalSize += size;
910                }
911                break;
912             }
913             case unionMember:
914             case structMember:
915             {
916                OldList * specs = MkList(), * list = MkList();
917
918                size = 0;
919                AddMembers(list, (Class)member, true, &size, topClass, null);
920                ListAdd(specs,
921                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
922                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
923                alignment = member.structAlignment;
924
925                if(alignment)
926                {
927                   if(totalSize % alignment)
928                      totalSize += alignment - (totalSize % alignment);
929                }
930                totalSize += size;
931                break;
932             }
933          }
934       }
935    }
936    if(retSize)
937    {
938       if(topMember && topMember.type == unionMember)
939          *retSize = Max(*retSize, totalSize);
940       else
941          *retSize += totalSize;
942    }
943    else if(totalSize < maxSize && _class.type != systemClass)
944    {
945       int autoPadding = 0;
946       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
947          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
948       if(totalSize + autoPadding < maxSize)
949       {
950          char sizeString[50];
951          sprintf(sizeString, "%d", maxSize - totalSize);
952          ListAdd(declarations,
953             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
954             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
955          if(addedPadding)
956             *addedPadding = true;
957       }
958    }
959    if(context)
960       FinishTemplatesContext(context);
961    return topMember ? topMember.memberID : _class.memberID;
962 }
963
964 static int DeclareMembers(Class _class, bool isMember)
965 {
966    DataMember topMember = isMember ? (DataMember) _class : null;
967    uint totalSize = 0;
968    DataMember member;
969    Context context = isMember ? null : SetupTemplatesContext(_class);
970
971    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
972       DeclareMembers(_class.base, false);
973
974    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
975    {
976       if(!member.isProperty)
977       {
978          switch(member.type)
979          {
980             case normalMember:
981             {
982                /*
983                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
984                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
985                   DeclareStruct(member.dataType._class.string, false);
986                   */
987                if(!member.dataType && member.dataTypeString)
988                   member.dataType = ProcessTypeString(member.dataTypeString, false);
989                if(member.dataType)
990                   DeclareType(member.dataType, false, false);
991                break;
992             }
993             case unionMember:
994             case structMember:
995             {
996                DeclareMembers((Class)member, true);
997                break;
998             }
999          }
1000       }
1001    }
1002    if(context)
1003       FinishTemplatesContext(context);
1004
1005    return topMember ? topMember.memberID : _class.memberID;
1006 }
1007
1008 void DeclareStruct(char * name, bool skipNoHead)
1009 {
1010    External external = null;
1011    Symbol classSym = FindClass(name);
1012
1013    if(!inCompiler || !classSym) return;
1014
1015    // We don't need any declaration for bit classes...
1016    if(classSym.registered &&
1017       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1018       return;
1019
1020    /*if(classSym.registered.templateClass)
1021       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1022    */
1023
1024    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1025    {
1026       // Add typedef struct
1027       Declaration decl;
1028       OldList * specifiers, * declarators;
1029       OldList * declarations = null;
1030       char structName[1024];
1031       external = (classSym.registered && classSym.registered.type == structClass) ?
1032          classSym.pointerExternal : classSym.structExternal;
1033
1034       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1035       // Moved this one up because DeclareClass done later will need it
1036
1037       classSym.declaring++;
1038
1039       if(strchr(classSym.string, '<'))
1040       {
1041          if(classSym.registered.templateClass)
1042          {
1043             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1044             classSym.declaring--;
1045          }
1046          return;
1047       }
1048
1049       //if(!skipNoHead)
1050          DeclareMembers(classSym.registered, false);
1051
1052       structName[0] = 0;
1053       FullClassNameCat(structName, name, false);
1054
1055       /*if(!external)
1056          external = MkExternalDeclaration(null);*/
1057
1058       if(!skipNoHead)
1059       {
1060          bool addedPadding = false;
1061          classSym.declaredStructSym = true;
1062
1063          declarations = MkList();
1064
1065          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1066
1067          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1068          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1069
1070          if(!declarations->count || (declarations->count == 1 && addedPadding))
1071          {
1072             FreeList(declarations, FreeClassDef);
1073             declarations = null;
1074          }
1075       }
1076       if(skipNoHead || declarations)
1077       {
1078          if(external && external.declaration)
1079          {
1080             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1081
1082             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1083             {
1084                // TODO: Fix this
1085                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1086
1087                // DANGER
1088                if(classSym.structExternal)
1089                   ast->Move(classSym.structExternal, curExternal.prev);
1090                ast->Move(classSym.pointerExternal, curExternal.prev);
1091
1092                classSym.id = curExternal.symbol.idCode;
1093                classSym.idCode = curExternal.symbol.idCode;
1094                // external = classSym.pointerExternal;
1095                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1096             }
1097          }
1098          else
1099          {
1100             if(!external)
1101                external = MkExternalDeclaration(null);
1102
1103             specifiers = MkList();
1104             declarators = MkList();
1105             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1106
1107             /*
1108             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1109             ListAdd(declarators, MkInitDeclarator(d, null));
1110             */
1111             external.declaration = decl = MkDeclaration(specifiers, declarators);
1112             if(decl.symbol && !decl.symbol.pointerExternal)
1113                decl.symbol.pointerExternal = external;
1114
1115             // For simple classes, keep the declaration as the external to move around
1116             if(classSym.registered && classSym.registered.type == structClass)
1117             {
1118                char className[1024];
1119                strcpy(className, "__ecereClass_");
1120                FullClassNameCat(className, classSym.string, true);
1121                MangleClassName(className);
1122
1123                // Testing This
1124                DeclareClass(classSym, className);
1125
1126                external.symbol = classSym;
1127                classSym.pointerExternal = external;
1128                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1129                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1130             }
1131             else
1132             {
1133                char className[1024];
1134                strcpy(className, "__ecereClass_");
1135                FullClassNameCat(className, classSym.string, true);
1136                MangleClassName(className);
1137
1138                // TOFIX: TESTING THIS...
1139                classSym.structExternal = external;
1140                DeclareClass(classSym, className);
1141                external.symbol = classSym;
1142             }
1143
1144             //if(curExternal)
1145                ast->Insert(curExternal ? curExternal.prev : null, external);
1146          }
1147       }
1148
1149       classSym.declaring--;
1150    }
1151    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1152    {
1153       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1154       // Moved this one up because DeclareClass done later will need it
1155
1156       // TESTING THIS:
1157       classSym.declaring++;
1158
1159       //if(!skipNoHead)
1160       {
1161          if(classSym.registered)
1162             DeclareMembers(classSym.registered, false);
1163       }
1164
1165       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1166       {
1167          // TODO: Fix this
1168          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1169
1170          // DANGER
1171          if(classSym.structExternal)
1172             ast->Move(classSym.structExternal, curExternal.prev);
1173          ast->Move(classSym.pointerExternal, curExternal.prev);
1174
1175          classSym.id = curExternal.symbol.idCode;
1176          classSym.idCode = curExternal.symbol.idCode;
1177          // external = classSym.pointerExternal;
1178          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1179       }
1180
1181       classSym.declaring--;
1182    }
1183    //return external;
1184 }
1185
1186 void DeclareProperty(Property prop, char * setName, char * getName)
1187 {
1188    Symbol symbol = prop.symbol;
1189    char propName[1024];
1190
1191    strcpy(setName, "__ecereProp_");
1192    FullClassNameCat(setName, prop._class.fullName, false);
1193    strcat(setName, "_Set_");
1194    // strcat(setName, prop.name);
1195    FullClassNameCat(setName, prop.name, true);
1196
1197    strcpy(getName, "__ecereProp_");
1198    FullClassNameCat(getName, prop._class.fullName, false);
1199    strcat(getName, "_Get_");
1200    FullClassNameCat(getName, prop.name, true);
1201    // strcat(getName, prop.name);
1202
1203    strcpy(propName, "__ecereProp_");
1204    FullClassNameCat(propName, prop._class.fullName, false);
1205    strcat(propName, "_");
1206    FullClassNameCat(propName, prop.name, true);
1207    // strcat(propName, prop.name);
1208
1209    // To support "char *" property
1210    MangleClassName(getName);
1211    MangleClassName(setName);
1212    MangleClassName(propName);
1213
1214    if(prop._class.type == structClass)
1215       DeclareStruct(prop._class.fullName, false);
1216
1217    if(!symbol || curExternal.symbol.idCode < symbol.id)
1218    {
1219       bool imported = false;
1220       bool dllImport = false;
1221       if(!symbol || symbol._import)
1222       {
1223          if(!symbol)
1224          {
1225             Symbol classSym;
1226             if(!prop._class.symbol)
1227                prop._class.symbol = FindClass(prop._class.fullName);
1228             classSym = prop._class.symbol;
1229             if(classSym && !classSym._import)
1230             {
1231                ModuleImport module;
1232
1233                if(prop._class.module)
1234                   module = FindModule(prop._class.module);
1235                else
1236                   module = mainModule;
1237
1238                classSym._import = ClassImport
1239                {
1240                   name = CopyString(prop._class.fullName);
1241                   isRemote = prop._class.isRemote;
1242                };
1243                module.classes.Add(classSym._import);
1244             }
1245             symbol = prop.symbol = Symbol { };
1246             symbol._import = (ClassImport)PropertyImport
1247             {
1248                name = CopyString(prop.name);
1249                isVirtual = false; //prop.isVirtual;
1250                hasSet = prop.Set ? true : false;
1251                hasGet = prop.Get ? true : false;
1252             };
1253             if(classSym)
1254                classSym._import.properties.Add(symbol._import);
1255          }
1256          imported = true;
1257          if(prop._class.module != privateModule && prop._class.module.importType != staticImport)
1258             dllImport = true;
1259       }
1260
1261       if(!symbol.type)
1262       {
1263          Context context = SetupTemplatesContext(prop._class);
1264          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1265          FinishTemplatesContext(context);
1266       }
1267
1268       // Get
1269       if(prop.Get)
1270       {
1271          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1272          {
1273             Declaration decl;
1274             OldList * specifiers, * declarators;
1275             Declarator d;
1276             OldList * params;
1277             Specifier spec;
1278             External external;
1279             Declarator typeDecl;
1280             bool simple = false;
1281
1282             specifiers = MkList();
1283             declarators = MkList();
1284             params = MkList();
1285
1286             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1287                MkDeclaratorIdentifier(MkIdentifier("this"))));
1288
1289             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1290             //if(imported)
1291             if(dllImport)
1292                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1293
1294             {
1295                Context context = SetupTemplatesContext(prop._class);
1296                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1297                FinishTemplatesContext(context);
1298             }
1299
1300             // Make sure the simple _class's type is declared
1301             for(spec = specifiers->first; spec; spec = spec.next)
1302             {
1303                if(spec.type == nameSpecifier /*SpecifierClass*/)
1304                {
1305                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1306                   {
1307                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1308                      symbol._class = classSym.registered;
1309                      if(classSym.registered && classSym.registered.type == structClass)
1310                      {
1311                         DeclareStruct(spec.name, false);
1312                         simple = true;
1313                      }
1314                   }
1315                }
1316             }
1317
1318             if(!simple)
1319                d = PlugDeclarator(typeDecl, d);
1320             else
1321             {
1322                ListAdd(params, MkTypeName(specifiers,
1323                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1324                specifiers = MkList();
1325             }
1326
1327             d = MkDeclaratorFunction(d, params);
1328
1329             //if(imported)
1330             if(dllImport)
1331                specifiers->Insert(null, MkSpecifier(EXTERN));
1332             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1333                specifiers->Insert(null, MkSpecifier(STATIC));
1334             if(simple)
1335                ListAdd(specifiers, MkSpecifier(VOID));
1336
1337             ListAdd(declarators, MkInitDeclarator(d, null));
1338
1339             decl = MkDeclaration(specifiers, declarators);
1340
1341             external = MkExternalDeclaration(decl);
1342             ast->Insert(curExternal.prev, external);
1343             external.symbol = symbol;
1344             symbol.externalGet = external;
1345
1346             ReplaceThisClassSpecifiers(specifiers, prop._class);
1347
1348             if(typeDecl)
1349                FreeDeclarator(typeDecl);
1350          }
1351          else
1352          {
1353             // Move declaration higher...
1354             ast->Move(symbol.externalGet, curExternal.prev);
1355          }
1356       }
1357
1358       // Set
1359       if(prop.Set)
1360       {
1361          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1362          {
1363             Declaration decl;
1364             OldList * specifiers, * declarators;
1365             Declarator d;
1366             OldList * params;
1367             Specifier spec;
1368             External external;
1369             Declarator typeDecl;
1370
1371             declarators = MkList();
1372             params = MkList();
1373
1374             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1375             if(!prop.conversion || prop._class.type == structClass)
1376             {
1377                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1378                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1379             }
1380
1381             specifiers = MkList();
1382
1383             {
1384                Context context = SetupTemplatesContext(prop._class);
1385                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1386                   MkDeclaratorIdentifier(MkIdentifier("value")));
1387                FinishTemplatesContext(context);
1388             }
1389             ListAdd(params, MkTypeName(specifiers, d));
1390
1391             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1392             //if(imported)
1393             if(dllImport)
1394                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1395             d = MkDeclaratorFunction(d, params);
1396
1397             // Make sure the simple _class's type is declared
1398             for(spec = specifiers->first; spec; spec = spec.next)
1399             {
1400                if(spec.type == nameSpecifier /*SpecifierClass*/)
1401                {
1402                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1403                   {
1404                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1405                      symbol._class = classSym.registered;
1406                      if(classSym.registered && classSym.registered.type == structClass)
1407                         DeclareStruct(spec.name, false);
1408                   }
1409                }
1410             }
1411
1412             ListAdd(declarators, MkInitDeclarator(d, null));
1413
1414             specifiers = MkList();
1415             //if(imported)
1416             if(dllImport)
1417                specifiers->Insert(null, MkSpecifier(EXTERN));
1418             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1419                specifiers->Insert(null, MkSpecifier(STATIC));
1420
1421             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1422             if(!prop.conversion || prop._class.type == structClass)
1423                ListAdd(specifiers, MkSpecifier(VOID));
1424             else
1425                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1426
1427             decl = MkDeclaration(specifiers, declarators);
1428
1429             external = MkExternalDeclaration(decl);
1430             ast->Insert(curExternal.prev, external);
1431             external.symbol = symbol;
1432             symbol.externalSet = external;
1433
1434             ReplaceThisClassSpecifiers(specifiers, prop._class);
1435          }
1436          else
1437          {
1438             // Move declaration higher...
1439             ast->Move(symbol.externalSet, curExternal.prev);
1440          }
1441       }
1442
1443       // Property (for Watchers)
1444       if(!symbol.externalPtr)
1445       {
1446          Declaration decl;
1447          External external;
1448          OldList * specifiers = MkList();
1449
1450          if(imported)
1451             specifiers->Insert(null, MkSpecifier(EXTERN));
1452          else
1453             specifiers->Insert(null, MkSpecifier(STATIC));
1454
1455          ListAdd(specifiers, MkSpecifierName("Property"));
1456
1457          {
1458             OldList * list = MkList();
1459             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1460                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1461
1462             if(!imported)
1463             {
1464                strcpy(propName, "__ecerePropM_");
1465                FullClassNameCat(propName, prop._class.fullName, false);
1466                strcat(propName, "_");
1467                // strcat(propName, prop.name);
1468                FullClassNameCat(propName, prop.name, true);
1469
1470                MangleClassName(propName);
1471
1472                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1473                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1474             }
1475             decl = MkDeclaration(specifiers, list);
1476          }
1477
1478          external = MkExternalDeclaration(decl);
1479          ast->Insert(curExternal.prev, external);
1480          external.symbol = symbol;
1481          symbol.externalPtr = external;
1482       }
1483       else
1484       {
1485          // Move declaration higher...
1486          ast->Move(symbol.externalPtr, curExternal.prev);
1487       }
1488
1489       symbol.id = curExternal.symbol.idCode;
1490    }
1491 }
1492
1493 // ***************** EXPRESSION PROCESSING ***************************
1494 public Type Dereference(Type source)
1495 {
1496    Type type = null;
1497    if(source)
1498    {
1499       if(source.kind == pointerType || source.kind == arrayType)
1500       {
1501          type = source.type;
1502          source.type.refCount++;
1503       }
1504       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1505       {
1506          type = Type
1507          {
1508             kind = charType;
1509             refCount = 1;
1510          };
1511       }
1512       // Support dereferencing of no head classes for now...
1513       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1514       {
1515          type = source;
1516          source.refCount++;
1517       }
1518       else
1519          Compiler_Error($"cannot dereference type\n");
1520    }
1521    return type;
1522 }
1523
1524 static Type Reference(Type source)
1525 {
1526    Type type = null;
1527    if(source)
1528    {
1529       type = Type
1530       {
1531          kind = pointerType;
1532          type = source;
1533          refCount = 1;
1534       };
1535       source.refCount++;
1536    }
1537    return type;
1538 }
1539
1540 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1541 {
1542    Identifier ident = member.identifiers ? member.identifiers->first : null;
1543    bool found = false;
1544    DataMember dataMember = null;
1545    Method method = null;
1546    bool freeType = false;
1547
1548    yylloc = member.loc;
1549
1550    if(!ident)
1551    {
1552       if(curMember)
1553       {
1554          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1555          if(*curMember)
1556          {
1557             found = true;
1558             dataMember = *curMember;
1559          }
1560       }
1561    }
1562    else
1563    {
1564       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1565       DataMember _subMemberStack[256];
1566       int _subMemberStackPos = 0;
1567
1568       // FILL MEMBER STACK
1569       if(!thisMember)
1570          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1571       if(thisMember)
1572       {
1573          dataMember = thisMember;
1574          if(curMember && thisMember.memberAccess == publicAccess)
1575          {
1576             *curMember = thisMember;
1577             *curClass = thisMember._class;
1578             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1579             *subMemberStackPos = _subMemberStackPos;
1580          }
1581          found = true;
1582       }
1583       else
1584       {
1585          // Setting a method
1586          method = eClass_FindMethod(_class, ident.string, privateModule);
1587          if(method && method.type == virtualMethod)
1588             found = true;
1589          else
1590             method = null;
1591       }
1592    }
1593
1594    if(found)
1595    {
1596       Type type = null;
1597       if(dataMember)
1598       {
1599          if(!dataMember.dataType && dataMember.dataTypeString)
1600          {
1601             //Context context = SetupTemplatesContext(dataMember._class);
1602             Context context = SetupTemplatesContext(_class);
1603             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1604             FinishTemplatesContext(context);
1605          }
1606          type = dataMember.dataType;
1607       }
1608       else if(method)
1609       {
1610          // This is for destination type...
1611          if(!method.dataType)
1612             ProcessMethodType(method);
1613          //DeclareMethod(method);
1614          // method.dataType = ((Symbol)method.symbol)->type;
1615          type = method.dataType;
1616       }
1617
1618       if(ident && ident.next)
1619       {
1620          for(ident = ident.next; ident && type; ident = ident.next)
1621          {
1622             if(type.kind == classType)
1623             {
1624                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1625                if(!dataMember)
1626                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1627                if(dataMember)
1628                   type = dataMember.dataType;
1629             }
1630             else if(type.kind == structType || type.kind == unionType)
1631             {
1632                Type memberType;
1633                for(memberType = type.members.first; memberType; memberType = memberType.next)
1634                {
1635                   if(!strcmp(memberType.name, ident.string))
1636                   {
1637                      type = memberType;
1638                      break;
1639                   }
1640                }
1641             }
1642          }
1643       }
1644
1645       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1646       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1647       {
1648          int id = 0;
1649          ClassTemplateParameter curParam = null;
1650          Class sClass;
1651          for(sClass = _class; sClass; sClass = sClass.base)
1652          {
1653             id = 0;
1654             if(sClass.templateClass) sClass = sClass.templateClass;
1655             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1656             {
1657                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1658                {
1659                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1660                   {
1661                      if(sClass.templateClass) sClass = sClass.templateClass;
1662                      id += sClass.templateParams.count;
1663                   }
1664                   break;
1665                }
1666                id++;
1667             }
1668             if(curParam) break;
1669          }
1670
1671          if(curParam)
1672          {
1673             ClassTemplateArgument arg = _class.templateArgs[id];
1674             if(arg.dataTypeString)
1675             {
1676                // FreeType(type);
1677                type = ProcessTypeString(arg.dataTypeString, false);
1678                freeType = true;
1679                if(type && _class.templateClass)
1680                   type.passAsTemplate = true;
1681                if(type)
1682                {
1683                   // type.refCount++;
1684                   /*if(!exp.destType)
1685                   {
1686                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1687                      exp.destType.refCount++;
1688                   }*/
1689                }
1690             }
1691          }
1692       }
1693       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1694       {
1695          Class expClass = type._class.registered;
1696          Class cClass = null;
1697          int c;
1698          int paramCount = 0;
1699          int lastParam = -1;
1700
1701          char templateString[1024];
1702          ClassTemplateParameter param;
1703          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1704          for(cClass = expClass; cClass; cClass = cClass.base)
1705          {
1706             int p = 0;
1707             if(cClass.templateClass) cClass = cClass.templateClass;
1708             for(param = cClass.templateParams.first; param; param = param.next)
1709             {
1710                int id = p;
1711                Class sClass;
1712                ClassTemplateArgument arg;
1713                for(sClass = cClass.base; sClass; sClass = sClass.base)
1714                {
1715                   if(sClass.templateClass) sClass = sClass.templateClass;
1716                   id += sClass.templateParams.count;
1717                }
1718                arg = expClass.templateArgs[id];
1719
1720                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1721                {
1722                   ClassTemplateParameter cParam;
1723                   //int p = numParams - sClass.templateParams.count;
1724                   int p = 0;
1725                   Class nextClass;
1726                   if(sClass.templateClass) sClass = sClass.templateClass;
1727
1728                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1729                   {
1730                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1731                      p += nextClass.templateParams.count;
1732                   }
1733
1734                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1735                   {
1736                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1737                      {
1738                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1739                         {
1740                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1741                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1742                            break;
1743                         }
1744                      }
1745                   }
1746                }
1747
1748                {
1749                   char argument[256];
1750                   argument[0] = '\0';
1751                   /*if(arg.name)
1752                   {
1753                      strcat(argument, arg.name.string);
1754                      strcat(argument, " = ");
1755                   }*/
1756                   switch(param.type)
1757                   {
1758                      case expression:
1759                      {
1760                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1761                         char expString[1024];
1762                         OldList * specs = MkList();
1763                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1764                         Expression exp;
1765                         char * string = PrintHexUInt64(arg.expression.ui64);
1766                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1767                         delete string;
1768
1769                         ProcessExpressionType(exp);
1770                         ComputeExpression(exp);
1771                         expString[0] = '\0';
1772                         PrintExpression(exp, expString);
1773                         strcat(argument, expString);
1774                         //delete exp;
1775                         FreeExpression(exp);
1776                         break;
1777                      }
1778                      case identifier:
1779                      {
1780                         strcat(argument, arg.member.name);
1781                         break;
1782                      }
1783                      case TemplateParameterType::type:
1784                      {
1785                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1786                            strcat(argument, arg.dataTypeString);
1787                         break;
1788                      }
1789                   }
1790                   if(argument[0])
1791                   {
1792                      if(paramCount) strcat(templateString, ", ");
1793                      if(lastParam != p - 1)
1794                      {
1795                         strcat(templateString, param.name);
1796                         strcat(templateString, " = ");
1797                      }
1798                      strcat(templateString, argument);
1799                      paramCount++;
1800                      lastParam = p;
1801                   }
1802                   p++;
1803                }
1804             }
1805          }
1806          {
1807             int len = strlen(templateString);
1808             if(templateString[len-1] == '<')
1809                len--;
1810             else
1811             {
1812                if(templateString[len-1] == '>')
1813                   templateString[len++] = ' ';
1814                templateString[len++] = '>';
1815             }
1816             templateString[len++] = '\0';
1817          }
1818          {
1819             Context context = SetupTemplatesContext(_class);
1820             if(freeType) FreeType(type);
1821             type = ProcessTypeString(templateString, false);
1822             freeType = true;
1823             FinishTemplatesContext(context);
1824          }
1825       }
1826
1827       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1828       {
1829          ProcessExpressionType(member.initializer.exp);
1830          if(!member.initializer.exp.expType)
1831          {
1832             if(inCompiler)
1833             {
1834                char expString[10240];
1835                expString[0] = '\0';
1836                PrintExpression(member.initializer.exp, expString);
1837                ChangeCh(expString, '\n', ' ');
1838                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1839             }
1840          }
1841          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1842          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1843          {
1844             Compiler_Error($"incompatible instance method %s\n", ident.string);
1845          }
1846       }
1847       else if(member.initializer)
1848       {
1849          /*
1850          FreeType(member.exp.destType);
1851          member.exp.destType = type;
1852          if(member.exp.destType)
1853             member.exp.destType.refCount++;
1854          ProcessExpressionType(member.exp);
1855          */
1856
1857          ProcessInitializer(member.initializer, type);
1858       }
1859       if(freeType) FreeType(type);
1860    }
1861    else
1862    {
1863       if(_class && _class.type == unitClass)
1864       {
1865          if(member.initializer)
1866          {
1867             /*
1868             FreeType(member.exp.destType);
1869             member.exp.destType = MkClassType(_class.fullName);
1870             ProcessExpressionType(member.initializer, type);
1871             */
1872             Type type = MkClassType(_class.fullName);
1873             ProcessInitializer(member.initializer, type);
1874             FreeType(type);
1875          }
1876       }
1877       else
1878       {
1879          if(member.initializer)
1880          {
1881             //ProcessExpressionType(member.exp);
1882             ProcessInitializer(member.initializer, null);
1883          }
1884          if(ident)
1885          {
1886             if(method)
1887             {
1888                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1889             }
1890             else if(_class)
1891             {
1892                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1893                if(inCompiler)
1894                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1895             }
1896          }
1897          else if(_class)
1898             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1899       }
1900    }
1901 }
1902
1903 void ProcessInstantiationType(Instantiation inst)
1904 {
1905    yylloc = inst.loc;
1906    if(inst._class)
1907    {
1908       MembersInit members;
1909       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1910       Class _class;
1911
1912       /*if(!inst._class.symbol)
1913          inst._class.symbol = FindClass(inst._class.name);*/
1914       classSym = inst._class.symbol;
1915       _class = classSym ? classSym.registered : null;
1916
1917       // DANGER: Patch for mutex not declaring its struct when not needed
1918       if(!_class || _class.type != noHeadClass)
1919          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1920
1921       afterExternal = afterExternal ? afterExternal : curExternal;
1922
1923       if(inst.exp)
1924          ProcessExpressionType(inst.exp);
1925
1926       inst.isConstant = true;
1927       if(inst.members)
1928       {
1929          DataMember curMember = null;
1930          Class curClass = null;
1931          DataMember subMemberStack[256];
1932          int subMemberStackPos = 0;
1933
1934          for(members = inst.members->first; members; members = members.next)
1935          {
1936             switch(members.type)
1937             {
1938                case methodMembersInit:
1939                {
1940                   char name[1024];
1941                   static uint instMethodID = 0;
1942                   External external = curExternal;
1943                   Context context = curContext;
1944                   Declarator declarator = members.function.declarator;
1945                   Identifier nameID = GetDeclId(declarator);
1946                   char * unmangled = nameID ? nameID.string : null;
1947                   Expression exp;
1948                   External createdExternal = null;
1949
1950                   if(inCompiler)
1951                   {
1952                      char number[16];
1953                      //members.function.dontMangle = true;
1954                      strcpy(name, "__ecereInstMeth_");
1955                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1956                      strcat(name, "_");
1957                      strcat(name, nameID.string);
1958                      strcat(name, "_");
1959                      sprintf(number, "_%08d", instMethodID++);
1960                      strcat(name, number);
1961                      nameID.string = CopyString(name);
1962                   }
1963
1964                   // Do modifications here...
1965                   if(declarator)
1966                   {
1967                      Symbol symbol = declarator.symbol;
1968                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
1969
1970                      if(method && method.type == virtualMethod)
1971                      {
1972                         symbol.method = method;
1973                         ProcessMethodType(method);
1974
1975                         if(!symbol.type.thisClass)
1976                         {
1977                            if(method.dataType.thisClass && currentClass &&
1978                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
1979                            {
1980                               if(!currentClass.symbol)
1981                                  currentClass.symbol = FindClass(currentClass.fullName);
1982                               symbol.type.thisClass = currentClass.symbol;
1983                            }
1984                            else
1985                            {
1986                               if(!_class.symbol)
1987                                  _class.symbol = FindClass(_class.fullName);
1988                               symbol.type.thisClass = _class.symbol;
1989                            }
1990                         }
1991                         // TESTING THIS HERE:
1992                         DeclareType(symbol.type, true, true);
1993
1994                      }
1995                      else if(classSym)
1996                      {
1997                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
1998                            unmangled, classSym.string);
1999                      }
2000                   }
2001
2002                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2003                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2004
2005                   if(nameID)
2006                   {
2007                      FreeSpecifier(nameID._class);
2008                      nameID._class = null;
2009                   }
2010
2011                   if(inCompiler)
2012                   {
2013
2014                      Type type = declarator.symbol.type;
2015                      External oldExternal = curExternal;
2016
2017                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2018                      // *** It was commented out for problems such as
2019                      /*
2020                            class VirtualDesktop : Window
2021                            {
2022                               clientSize = Size { };
2023                               Timer timer
2024                               {
2025                                  bool DelayExpired()
2026                                  {
2027                                     clientSize.w;
2028                                     return true;
2029                                  }
2030                               };
2031                            }
2032                      */
2033                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2034
2035                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2036
2037                      /*
2038                      if(strcmp(declarator.symbol.string, name))
2039                      {
2040                         printf("TOCHECK: Look out for this\n");
2041                         delete declarator.symbol.string;
2042                         declarator.symbol.string = CopyString(name);
2043                      }
2044
2045                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2046                      {
2047                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2048                         excludedSymbols->Remove(declarator.symbol);
2049                         globalContext.symbols.Add((BTNode)declarator.symbol);
2050                         if(strstr(declarator.symbol.string), "::")
2051                            globalContext.hasNameSpace = true;
2052
2053                      }
2054                      */
2055
2056                      //curExternal = curExternal.prev;
2057                      //afterExternal = afterExternal->next;
2058
2059                      //ProcessFunction(afterExternal->function);
2060
2061                      //curExternal = afterExternal;
2062                      {
2063                         External externalDecl;
2064                         externalDecl = MkExternalDeclaration(null);
2065                         ast->Insert(oldExternal.prev, externalDecl);
2066
2067                         // Which function does this process?
2068                         if(createdExternal.function)
2069                         {
2070                            ProcessFunction(createdExternal.function);
2071
2072                            //curExternal = oldExternal;
2073
2074                            {
2075                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2076
2077                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2078                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2079
2080                               //externalDecl = MkExternalDeclaration(decl);
2081
2082                               //***** ast->Insert(external.prev, externalDecl);
2083                               //ast->Insert(curExternal.prev, externalDecl);
2084                               externalDecl.declaration = decl;
2085                               if(decl.symbol && !decl.symbol.pointerExternal)
2086                                  decl.symbol.pointerExternal = externalDecl;
2087
2088                               // Trying this out...
2089                               declarator.symbol.pointerExternal = externalDecl;
2090                            }
2091                         }
2092                      }
2093                   }
2094                   else if(declarator)
2095                   {
2096                      curExternal = declarator.symbol.pointerExternal;
2097                      ProcessFunction((FunctionDefinition)members.function);
2098                   }
2099                   curExternal = external;
2100                   curContext = context;
2101
2102                   if(inCompiler)
2103                   {
2104                      FreeClassFunction(members.function);
2105
2106                      // In this pass, turn this into a MemberInitData
2107                      exp = QMkExpId(name);
2108                      members.type = dataMembersInit;
2109                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2110
2111                      delete unmangled;
2112                   }
2113                   break;
2114                }
2115                case dataMembersInit:
2116                {
2117                   if(members.dataMembers && classSym)
2118                   {
2119                      MemberInit member;
2120                      Location oldyyloc = yylloc;
2121                      for(member = members.dataMembers->first; member; member = member.next)
2122                      {
2123                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2124                         if(member.initializer && !member.initializer.isConstant)
2125                            inst.isConstant = false;
2126                      }
2127                      yylloc = oldyyloc;
2128                   }
2129                   break;
2130                }
2131             }
2132          }
2133       }
2134    }
2135 }
2136
2137 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2138 {
2139    // OPTIMIZATIONS: TESTING THIS...
2140    if(inCompiler)
2141    {
2142       if(type.kind == functionType)
2143       {
2144          Type param;
2145          if(declareParams)
2146          {
2147             for(param = type.params.first; param; param = param.next)
2148                DeclareType(param, declarePointers, true);
2149          }
2150          DeclareType(type.returnType, declarePointers, true);
2151       }
2152       else if(type.kind == pointerType && declarePointers)
2153          DeclareType(type.type, declarePointers, false);
2154       else if(type.kind == classType)
2155       {
2156          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2157             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2158       }
2159       else if(type.kind == structType || type.kind == unionType)
2160       {
2161          Type member;
2162          for(member = type.members.first; member; member = member.next)
2163             DeclareType(member, false, false);
2164       }
2165       else if(type.kind == arrayType)
2166          DeclareType(type.arrayType, declarePointers, false);
2167    }
2168 }
2169
2170 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2171 {
2172    ClassTemplateArgument * arg = null;
2173    int id = 0;
2174    ClassTemplateParameter curParam = null;
2175    Class sClass;
2176    for(sClass = _class; sClass; sClass = sClass.base)
2177    {
2178       id = 0;
2179       if(sClass.templateClass) sClass = sClass.templateClass;
2180       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2181       {
2182          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2183          {
2184             for(sClass = sClass.base; sClass; sClass = sClass.base)
2185             {
2186                if(sClass.templateClass) sClass = sClass.templateClass;
2187                id += sClass.templateParams.count;
2188             }
2189             break;
2190          }
2191          id++;
2192       }
2193       if(curParam) break;
2194    }
2195    if(curParam)
2196    {
2197       arg = &_class.templateArgs[id];
2198       if(arg && param.type == type)
2199          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2200    }
2201    return arg;
2202 }
2203
2204 public Context SetupTemplatesContext(Class _class)
2205 {
2206    Context context = PushContext();
2207    context.templateTypesOnly = true;
2208    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2209    {
2210       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2211       for(; param; param = param.next)
2212       {
2213          if(param.type == type && param.identifier)
2214          {
2215             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2216             curContext.templateTypes.Add((BTNode)type);
2217          }
2218       }
2219    }
2220    else if(_class)
2221    {
2222       Class sClass;
2223       for(sClass = _class; sClass; sClass = sClass.base)
2224       {
2225          ClassTemplateParameter p;
2226          for(p = sClass.templateParams.first; p; p = p.next)
2227          {
2228             //OldList * specs = MkList();
2229             //Declarator decl = null;
2230             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2231             if(p.type == type)
2232             {
2233                TemplateParameter param = p.param;
2234                TemplatedType type;
2235                if(!param)
2236                {
2237                   // ADD DATA TYPE HERE...
2238                   p.param = param = TemplateParameter
2239                   {
2240                      identifier = MkIdentifier(p.name), type = p.type,
2241                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2242                   };
2243                }
2244                type = TemplatedType { key = (uintptr)p.name, param = param };
2245                curContext.templateTypes.Add((BTNode)type);
2246             }
2247          }
2248       }
2249    }
2250    return context;
2251 }
2252
2253 public void FinishTemplatesContext(Context context)
2254 {
2255    PopContext(context);
2256    FreeContext(context);
2257    delete context;
2258 }
2259
2260 public void ProcessMethodType(Method method)
2261 {
2262    if(!method.dataType)
2263    {
2264       Context context = SetupTemplatesContext(method._class);
2265
2266       method.dataType = ProcessTypeString(method.dataTypeString, false);
2267
2268       FinishTemplatesContext(context);
2269
2270       if(method.type != virtualMethod && method.dataType)
2271       {
2272          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2273          {
2274             if(!method._class.symbol)
2275                method._class.symbol = FindClass(method._class.fullName);
2276             method.dataType.thisClass = method._class.symbol;
2277          }
2278       }
2279
2280       // Why was this commented out? Working fine without now...
2281
2282       /*
2283       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2284          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2285          */
2286    }
2287
2288    /*
2289    if(type)
2290    {
2291       char * par = strstr(type, "(");
2292       char * classOp = null;
2293       int classOpLen = 0;
2294       if(par)
2295       {
2296          int c;
2297          for(c = par-type-1; c >= 0; c++)
2298          {
2299             if(type[c] == ':' && type[c+1] == ':')
2300             {
2301                classOp = type + c - 1;
2302                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2303                {
2304                   classOp--;
2305                   classOpLen++;
2306                }
2307                break;
2308             }
2309             else if(!isspace(type[c]))
2310                break;
2311          }
2312       }
2313       if(classOp)
2314       {
2315          char temp[1024];
2316          int typeLen = strlen(type);
2317          memcpy(temp, classOp, classOpLen);
2318          temp[classOpLen] = '\0';
2319          if(temp[0])
2320             _class = eSystem_FindClass(module, temp);
2321          else
2322             _class = null;
2323          method.dataTypeString = new char[typeLen - classOpLen + 1];
2324          memcpy(method.dataTypeString, type, classOp - type);
2325          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2326       }
2327       else
2328          method.dataTypeString = type;
2329    }
2330    */
2331 }
2332
2333
2334 public void ProcessPropertyType(Property prop)
2335 {
2336    if(!prop.dataType)
2337    {
2338       Context context = SetupTemplatesContext(prop._class);
2339       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2340       FinishTemplatesContext(context);
2341    }
2342 }
2343
2344 public void DeclareMethod(Method method, char * name)
2345 {
2346    Symbol symbol = method.symbol;
2347    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2348    {
2349       bool imported = false;
2350       bool dllImport = false;
2351
2352       if(!method.dataType)
2353          method.dataType = ProcessTypeString(method.dataTypeString, false);
2354
2355       if(!symbol || symbol._import || method.type == virtualMethod)
2356       {
2357          if(!symbol || method.type == virtualMethod)
2358          {
2359             Symbol classSym;
2360             if(!method._class.symbol)
2361                method._class.symbol = FindClass(method._class.fullName);
2362             classSym = method._class.symbol;
2363             if(!classSym._import)
2364             {
2365                ModuleImport module;
2366
2367                if(method._class.module && method._class.module.name)
2368                   module = FindModule(method._class.module);
2369                else
2370                   module = mainModule;
2371                classSym._import = ClassImport
2372                {
2373                   name = CopyString(method._class.fullName);
2374                   isRemote = method._class.isRemote;
2375                };
2376                module.classes.Add(classSym._import);
2377             }
2378             if(!symbol)
2379             {
2380                symbol = method.symbol = Symbol { };
2381             }
2382             if(!symbol._import)
2383             {
2384                symbol._import = (ClassImport)MethodImport
2385                {
2386                   name = CopyString(method.name);
2387                   isVirtual = method.type == virtualMethod;
2388                };
2389                classSym._import.methods.Add(symbol._import);
2390             }
2391             if(!symbol)
2392             {
2393                // Set the symbol type
2394                /*
2395                if(!type.thisClass)
2396                {
2397                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2398                }
2399                else if(type.thisClass == (void *)-1)
2400                {
2401                   type.thisClass = null;
2402                }
2403                */
2404                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2405                symbol.type = method.dataType;
2406                if(symbol.type) symbol.type.refCount++;
2407             }
2408             /*
2409             if(!method.thisClass || strcmp(method.thisClass, "void"))
2410                symbol.type.params.Insert(null,
2411                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2412             */
2413          }
2414          if(!method.dataType.dllExport)
2415          {
2416             imported = true;
2417             if(method._class.module != privateModule && method._class.module.importType != staticImport)
2418                dllImport = true;
2419          }
2420       }
2421
2422       /* MOVING THIS UP
2423       if(!method.dataType)
2424          method.dataType = ((Symbol)method.symbol).type;
2425          //ProcessMethodType(method);
2426       */
2427
2428       if(method.type != virtualMethod && method.dataType)
2429          DeclareType(method.dataType, true, true);
2430
2431       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2432       {
2433          // We need a declaration here :)
2434          Declaration decl;
2435          OldList * specifiers, * declarators;
2436          Declarator d;
2437          Declarator funcDecl;
2438          External external;
2439
2440          specifiers = MkList();
2441          declarators = MkList();
2442
2443          //if(imported)
2444          if(dllImport)
2445             ListAdd(specifiers, MkSpecifier(EXTERN));
2446          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2447             ListAdd(specifiers, MkSpecifier(STATIC));
2448
2449          if(method.type == virtualMethod)
2450          {
2451             ListAdd(specifiers, MkSpecifier(INT));
2452             d = MkDeclaratorIdentifier(MkIdentifier(name));
2453          }
2454          else
2455          {
2456             d = MkDeclaratorIdentifier(MkIdentifier(name));
2457             //if(imported)
2458             if(dllImport)
2459                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2460             {
2461                Context context = SetupTemplatesContext(method._class);
2462                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2463                FinishTemplatesContext(context);
2464             }
2465             funcDecl = GetFuncDecl(d);
2466
2467             if(dllImport)
2468             {
2469                Specifier spec, next;
2470                for(spec = specifiers->first; spec; spec = next)
2471                {
2472                   next = spec.next;
2473                   if(spec.type == extendedSpecifier)
2474                   {
2475                      specifiers->Remove(spec);
2476                      FreeSpecifier(spec);
2477                   }
2478                }
2479             }
2480
2481             // Add this parameter if not a static method
2482             if(method.dataType && !method.dataType.staticMethod)
2483             {
2484                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2485                {
2486                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2487                   TypeName thisParam = MkTypeName(MkListOne(
2488                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2489                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2490                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2491                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2492
2493                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2494                   {
2495                      TypeName param = funcDecl.function.parameters->first;
2496                      funcDecl.function.parameters->Remove(param);
2497                      FreeTypeName(param);
2498                   }
2499
2500                   if(!funcDecl.function.parameters)
2501                      funcDecl.function.parameters = MkList();
2502                   funcDecl.function.parameters->Insert(null, thisParam);
2503                }
2504             }
2505             // Make sure we don't have empty parameter declarations for static methods...
2506             /*
2507             else if(!funcDecl.function.parameters)
2508             {
2509                funcDecl.function.parameters = MkList();
2510                funcDecl.function.parameters->Insert(null,
2511                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2512             }*/
2513          }
2514          // TESTING THIS:
2515          ProcessDeclarator(d);
2516
2517          ListAdd(declarators, MkInitDeclarator(d, null));
2518
2519          decl = MkDeclaration(specifiers, declarators);
2520
2521          ReplaceThisClassSpecifiers(specifiers, method._class);
2522
2523          // Keep a different symbol for the function definition than the declaration...
2524          if(symbol.pointerExternal)
2525          {
2526             Symbol functionSymbol { };
2527
2528             // Copy symbol
2529             {
2530                *functionSymbol = *symbol;
2531                functionSymbol.string = CopyString(symbol.string);
2532                if(functionSymbol.type)
2533                   functionSymbol.type.refCount++;
2534             }
2535
2536             excludedSymbols->Add(functionSymbol);
2537             symbol.pointerExternal.symbol = functionSymbol;
2538          }
2539          external = MkExternalDeclaration(decl);
2540          if(curExternal)
2541             ast->Insert(curExternal ? curExternal.prev : null, external);
2542          external.symbol = symbol;
2543          symbol.pointerExternal = external;
2544       }
2545       else if(ast)
2546       {
2547          // Move declaration higher...
2548          ast->Move(symbol.pointerExternal, curExternal.prev);
2549       }
2550
2551       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2552    }
2553 }
2554
2555 char * ReplaceThisClass(Class _class)
2556 {
2557    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2558    {
2559       bool first = true;
2560       int p = 0;
2561       ClassTemplateParameter param;
2562       int lastParam = -1;
2563
2564       char className[1024];
2565       strcpy(className, _class.fullName);
2566       for(param = _class.templateParams.first; param; param = param.next)
2567       {
2568          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2569          {
2570             if(first) strcat(className, "<");
2571             if(!first) strcat(className, ", ");
2572             if(lastParam + 1 != p)
2573             {
2574                strcat(className, param.name);
2575                strcat(className, " = ");
2576             }
2577             strcat(className, param.name);
2578             first = false;
2579             lastParam = p;
2580          }
2581          p++;
2582       }
2583       if(!first)
2584       {
2585          int len = strlen(className);
2586          if(className[len-1] == '>') className[len++] = ' ';
2587          className[len++] = '>';
2588          className[len++] = '\0';
2589       }
2590       return CopyString(className);
2591    }
2592    else
2593       return CopyString(_class.fullName);
2594 }
2595
2596 Type ReplaceThisClassType(Class _class)
2597 {
2598    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2599    {
2600       bool first = true;
2601       int p = 0;
2602       ClassTemplateParameter param;
2603       int lastParam = -1;
2604       char className[1024];
2605       strcpy(className, _class.fullName);
2606
2607       for(param = _class.templateParams.first; param; param = param.next)
2608       {
2609          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2610          {
2611             if(first) strcat(className, "<");
2612             if(!first) strcat(className, ", ");
2613             if(lastParam + 1 != p)
2614             {
2615                strcat(className, param.name);
2616                strcat(className, " = ");
2617             }
2618             strcat(className, param.name);
2619             first = false;
2620             lastParam = p;
2621          }
2622          p++;
2623       }
2624       if(!first)
2625       {
2626          int len = strlen(className);
2627          if(className[len-1] == '>') className[len++] = ' ';
2628          className[len++] = '>';
2629          className[len++] = '\0';
2630       }
2631       return MkClassType(className);
2632       //return ProcessTypeString(className, false);
2633    }
2634    else
2635    {
2636       return MkClassType(_class.fullName);
2637       //return ProcessTypeString(_class.fullName, false);
2638    }
2639 }
2640
2641 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2642 {
2643    if(specs != null && _class)
2644    {
2645       Specifier spec;
2646       for(spec = specs.first; spec; spec = spec.next)
2647       {
2648          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2649          {
2650             spec.type = nameSpecifier;
2651             spec.name = ReplaceThisClass(_class);
2652             spec.symbol = FindClass(spec.name); //_class.symbol;
2653          }
2654       }
2655    }
2656 }
2657
2658 // Returns imported or not
2659 bool DeclareFunction(GlobalFunction function, char * name)
2660 {
2661    Symbol symbol = function.symbol;
2662    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2663    {
2664       bool imported = false;
2665       bool dllImport = false;
2666
2667       if(!function.dataType)
2668       {
2669          function.dataType = ProcessTypeString(function.dataTypeString, false);
2670          if(!function.dataType.thisClass)
2671             function.dataType.staticMethod = true;
2672       }
2673
2674       if(inCompiler)
2675       {
2676          if(!symbol)
2677          {
2678             ModuleImport module = FindModule(function.module);
2679             // WARNING: This is not added anywhere...
2680             symbol = function.symbol = Symbol {  };
2681
2682             if(module.name)
2683             {
2684                if(!function.dataType.dllExport)
2685                {
2686                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2687                   module.functions.Add(symbol._import);
2688                }
2689             }
2690             // Set the symbol type
2691             {
2692                symbol.type = ProcessTypeString(function.dataTypeString, false);
2693                if(!symbol.type.thisClass)
2694                   symbol.type.staticMethod = true;
2695             }
2696          }
2697          imported = symbol._import ? true : false;
2698          if(imported && function.module != privateModule && function.module.importType != staticImport)
2699             dllImport = true;
2700       }
2701
2702       DeclareType(function.dataType, true, true);
2703
2704       if(inCompiler)
2705       {
2706          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2707          {
2708             // We need a declaration here :)
2709             Declaration decl;
2710             OldList * specifiers, * declarators;
2711             Declarator d;
2712             Declarator funcDecl;
2713             External external;
2714
2715             specifiers = MkList();
2716             declarators = MkList();
2717
2718             //if(imported)
2719                ListAdd(specifiers, MkSpecifier(EXTERN));
2720             /*
2721             else
2722                ListAdd(specifiers, MkSpecifier(STATIC));
2723             */
2724
2725             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2726             //if(imported)
2727             if(dllImport)
2728                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2729
2730             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2731             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2732             if(function.module.importType == staticImport)
2733             {
2734                Specifier spec;
2735                for(spec = specifiers->first; spec; spec = spec.next)
2736                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2737                   {
2738                      specifiers->Remove(spec);
2739                      FreeSpecifier(spec);
2740                      break;
2741                   }
2742             }
2743
2744             funcDecl = GetFuncDecl(d);
2745
2746             // Make sure we don't have empty parameter declarations for static methods...
2747             if(funcDecl && !funcDecl.function.parameters)
2748             {
2749                funcDecl.function.parameters = MkList();
2750                funcDecl.function.parameters->Insert(null,
2751                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2752             }
2753
2754             ListAdd(declarators, MkInitDeclarator(d, null));
2755
2756             {
2757                Context oldCtx = curContext;
2758                curContext = globalContext;
2759                decl = MkDeclaration(specifiers, declarators);
2760                curContext = oldCtx;
2761             }
2762
2763             // Keep a different symbol for the function definition than the declaration...
2764             if(symbol.pointerExternal)
2765             {
2766                Symbol functionSymbol { };
2767                // Copy symbol
2768                {
2769                   *functionSymbol = *symbol;
2770                   functionSymbol.string = CopyString(symbol.string);
2771                   if(functionSymbol.type)
2772                      functionSymbol.type.refCount++;
2773                }
2774
2775                excludedSymbols->Add(functionSymbol);
2776
2777                symbol.pointerExternal.symbol = functionSymbol;
2778             }
2779             external = MkExternalDeclaration(decl);
2780             if(curExternal)
2781                ast->Insert(curExternal.prev, external);
2782             external.symbol = symbol;
2783             symbol.pointerExternal = external;
2784          }
2785          else
2786          {
2787             // Move declaration higher...
2788             ast->Move(symbol.pointerExternal, curExternal.prev);
2789          }
2790
2791          if(curExternal)
2792             symbol.id = curExternal.symbol.idCode;
2793       }
2794    }
2795    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2796 }
2797
2798 void DeclareGlobalData(GlobalData data)
2799 {
2800    Symbol symbol = data.symbol;
2801    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2802    {
2803       if(inCompiler)
2804       {
2805          if(!symbol)
2806             symbol = data.symbol = Symbol { };
2807       }
2808       if(!data.dataType)
2809          data.dataType = ProcessTypeString(data.dataTypeString, false);
2810       DeclareType(data.dataType, true, true);
2811       if(inCompiler)
2812       {
2813          if(!symbol.pointerExternal)
2814          {
2815             // We need a declaration here :)
2816             Declaration decl;
2817             OldList * specifiers, * declarators;
2818             Declarator d;
2819             External external;
2820
2821             specifiers = MkList();
2822             declarators = MkList();
2823
2824             ListAdd(specifiers, MkSpecifier(EXTERN));
2825             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2826             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2827
2828             ListAdd(declarators, MkInitDeclarator(d, null));
2829
2830             decl = MkDeclaration(specifiers, declarators);
2831             external = MkExternalDeclaration(decl);
2832             if(curExternal)
2833                ast->Insert(curExternal.prev, external);
2834             external.symbol = symbol;
2835             symbol.pointerExternal = external;
2836          }
2837          else
2838          {
2839             // Move declaration higher...
2840             ast->Move(symbol.pointerExternal, curExternal.prev);
2841          }
2842
2843          if(curExternal)
2844             symbol.id = curExternal.symbol.idCode;
2845       }
2846    }
2847 }
2848
2849 class Conversion : struct
2850 {
2851    Conversion prev, next;
2852    Property convert;
2853    bool isGet;
2854    Type resultType;
2855 };
2856
2857 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2858 {
2859    if(source && dest)
2860    {
2861       // Property convert;
2862
2863       if(source.kind == templateType && dest.kind != templateType)
2864       {
2865          Type type = ProcessTemplateParameterType(source.templateParameter);
2866          if(type) source = type;
2867       }
2868
2869       if(dest.kind == templateType && source.kind != templateType)
2870       {
2871          Type type = ProcessTemplateParameterType(dest.templateParameter);
2872          if(type) dest = type;
2873       }
2874
2875       if(dest.classObjectType == typedObject)
2876       {
2877          if(source.classObjectType != anyObject)
2878             return true;
2879          else
2880          {
2881             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2882             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2883             {
2884                return true;
2885             }
2886          }
2887       }
2888       else
2889       {
2890          if(source.classObjectType == anyObject)
2891             return true;
2892          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2893             return true;
2894       }
2895
2896       if((dest.kind == structType && source.kind == structType) ||
2897          (dest.kind == unionType && source.kind == unionType))
2898       {
2899          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2900              (source.members.first && source.members.first == dest.members.first))
2901             return true;
2902       }
2903
2904       if(dest.kind == ellipsisType && source.kind != voidType)
2905          return true;
2906
2907       if(dest.kind == pointerType && dest.type.kind == voidType &&
2908          ((source.kind == classType && (!source._class || !source._class.registered || source._class.registered.type == structClass || source._class.registered.type == normalClass || source._class.registered.type == noHeadClass || source._class.registered.type == systemClass))
2909          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2910
2911          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2912
2913          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2914          return true;
2915       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2916          ((dest.kind == classType && (!dest._class || !dest._class.registered || dest._class.registered.type == structClass || dest._class.registered.type == normalClass || dest._class.registered.type == noHeadClass || dest._class.registered.type == systemClass))
2917          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2918
2919          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2920
2921          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2922          return true;
2923
2924       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2925       {
2926          if(source._class.registered && source._class.registered.type == unitClass)
2927          {
2928             if(conversions != null)
2929             {
2930                if(source._class.registered == dest._class.registered)
2931                   return true;
2932             }
2933             else
2934             {
2935                Class sourceBase, destBase;
2936                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2937                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2938                if(sourceBase == destBase)
2939                   return true;
2940             }
2941          }
2942          // Don't match enum inheriting from other enum if resolving enumeration values
2943          // TESTING: !dest.classObjectType
2944          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2945             (enumBaseType ||
2946                (!source._class.registered || source._class.registered.type != enumClass) ||
2947                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2948             return true;
2949          else
2950          {
2951             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2952             if(enumBaseType &&
2953                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2954                ((source._class && source._class.registered && source._class.registered.type != enumClass) || source.kind == classType)) // Added this here for a base enum to be acceptable for a derived enum (#139)
2955             {
2956                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2957                {
2958                   return true;
2959                }
2960             }
2961          }
2962       }
2963
2964       // JUST ADDED THIS...
2965       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2966          return true;
2967
2968       if(doConversion)
2969       {
2970          // Just added this for Straight conversion of ColorAlpha => Color
2971          if(source.kind == classType)
2972          {
2973             Class _class;
2974             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2975             {
2976                Property convert;
2977                for(convert = _class.conversions.first; convert; convert = convert.next)
2978                {
2979                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2980                   {
2981                      Conversion after = (conversions != null) ? conversions.last : null;
2982
2983                      if(!convert.dataType)
2984                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
2985                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
2986                      {
2987                         if(!conversions && !convert.Get)
2988                            return true;
2989                         else if(conversions != null)
2990                         {
2991                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
2992                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
2993                               (dest.kind != classType || dest._class.registered != _class.base))
2994                               return true;
2995                            else
2996                            {
2997                               Conversion conv { convert = convert, isGet = true };
2998                               // conversions.Add(conv);
2999                               conversions.Insert(after, conv);
3000                               return true;
3001                            }
3002                         }
3003                      }
3004                   }
3005                }
3006             }
3007          }
3008
3009          // MOVING THIS??
3010
3011          if(dest.kind == classType)
3012          {
3013             Class _class;
3014             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3015             {
3016                Property convert;
3017                for(convert = _class.conversions.first; convert; convert = convert.next)
3018                {
3019                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3020                   {
3021                      // Conversion after = (conversions != null) ? conversions.last : null;
3022
3023                      if(!convert.dataType)
3024                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3025                      // Just added this equality check to prevent recursion.... Make it safer?
3026                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3027                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3028                      {
3029                         if(!conversions && !convert.Set)
3030                            return true;
3031                         else if(conversions != null)
3032                         {
3033                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3034                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3035                               (source.kind != classType || source._class.registered != _class.base))
3036                               return true;
3037                            else
3038                            {
3039                               // *** Testing this! ***
3040                               Conversion conv { convert = convert };
3041                               conversions.Add(conv);
3042                               //conversions.Insert(after, conv);
3043                               return true;
3044                            }
3045                         }
3046                      }
3047                   }
3048                }
3049             }
3050             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3051             {
3052                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3053                   (source.kind != classType || source._class.registered.type != structClass))
3054                   return true;
3055             }*/
3056
3057             // TESTING THIS... IS THIS OK??
3058             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3059             {
3060                if(!dest._class.registered.dataType)
3061                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3062                // Only support this for classes...
3063                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3064                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3065                {
3066                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3067                   {
3068                      return true;
3069                   }
3070                }
3071             }
3072          }
3073
3074          // Moved this lower
3075          if(source.kind == classType)
3076          {
3077             Class _class;
3078             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3079             {
3080                Property convert;
3081                for(convert = _class.conversions.first; convert; convert = convert.next)
3082                {
3083                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3084                   {
3085                      Conversion after = (conversions != null) ? conversions.last : null;
3086
3087                      if(!convert.dataType)
3088                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3089                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3090                      {
3091                         if(!conversions && !convert.Get)
3092                            return true;
3093                         else if(conversions != null)
3094                         {
3095                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3096                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3097                               (dest.kind != classType || dest._class.registered != _class.base))
3098                               return true;
3099                            else
3100                            {
3101                               Conversion conv { convert = convert, isGet = true };
3102
3103                               // conversions.Add(conv);
3104                               conversions.Insert(after, conv);
3105                               return true;
3106                            }
3107                         }
3108                      }
3109                   }
3110                }
3111             }
3112
3113             // TESTING THIS... IS THIS OK??
3114             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3115             {
3116                if(!source._class.registered.dataType)
3117                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3118                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3119                {
3120                   return true;
3121                }
3122             }
3123          }
3124       }
3125
3126       if(source.kind == classType || source.kind == subClassType)
3127          ;
3128       else if(dest.kind == source.kind &&
3129          (dest.kind != structType && dest.kind != unionType &&
3130           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3131           return true;
3132       // RECENTLY ADDED THESE
3133       else if(dest.kind == doubleType && source.kind == floatType)
3134          return true;
3135       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3136          return true;
3137       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3138          return true;
3139       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3140          return true;
3141       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3142          return true;
3143       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3144          return true;
3145       else if(source.kind == enumType &&
3146          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3147           return true;
3148       else if(dest.kind == enumType &&
3149          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3150           return true;
3151       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3152               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3153       {
3154          Type paramSource, paramDest;
3155
3156          if(dest.kind == methodType)
3157             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3158          if(source.kind == methodType)
3159             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3160
3161          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3162          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3163          if(dest.kind == methodType)
3164             dest = dest.method.dataType;
3165          if(source.kind == methodType)
3166             source = source.method.dataType;
3167
3168          paramSource = source.params.first;
3169          if(paramSource && paramSource.kind == voidType) paramSource = null;
3170          paramDest = dest.params.first;
3171          if(paramDest && paramDest.kind == voidType) paramDest = null;
3172
3173
3174          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3175             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3176          {
3177             // Source thisClass must be derived from destination thisClass
3178             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3179                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3180             {
3181                if(paramDest && paramDest.kind == classType)
3182                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3183                else
3184                   Compiler_Error($"method class should not take an object\n");
3185                return false;
3186             }
3187             paramDest = paramDest.next;
3188          }
3189          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3190          {
3191             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3192             {
3193                if(dest.thisClass)
3194                {
3195                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3196                   {
3197                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3198                      return false;
3199                   }
3200                }
3201                else
3202                {
3203                   // THIS WAS BACKWARDS:
3204                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3205                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3206                   {
3207                      if(owningClassDest)
3208                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3209                      else
3210                         Compiler_Error($"overriding class expected to be derived from method class\n");
3211                      return false;
3212                   }
3213                }
3214                paramSource = paramSource.next;
3215             }
3216             else
3217             {
3218                if(dest.thisClass)
3219                {
3220                   // Source thisClass must be derived from destination thisClass
3221                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3222                   {
3223                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3224                      return false;
3225                   }
3226                }
3227                else
3228                {
3229                   // THIS WAS BACKWARDS TOO??
3230                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3231                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3232                   {
3233                      //if(owningClass)
3234                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3235                      //else
3236                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3237                      return false;
3238                   }
3239                }
3240             }
3241          }
3242
3243
3244          // Source return type must be derived from destination return type
3245          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3246          {
3247             Compiler_Warning($"incompatible return type for function\n");
3248             return false;
3249          }
3250
3251          // Check parameters
3252
3253          for(; paramDest; paramDest = paramDest.next)
3254          {
3255             if(!paramSource)
3256             {
3257                //Compiler_Warning($"not enough parameters\n");
3258                Compiler_Error($"not enough parameters\n");
3259                return false;
3260             }
3261             {
3262                Type paramDestType = paramDest;
3263                Type paramSourceType = paramSource;
3264                Type type = paramDestType;
3265
3266                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3267                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3268                   paramSource.kind != templateType)
3269                {
3270                   int id = 0;
3271                   ClassTemplateParameter curParam = null;
3272                   Class sClass;
3273                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3274                   {
3275                      id = 0;
3276                      if(sClass.templateClass) sClass = sClass.templateClass;
3277                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3278                      {
3279                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3280                         {
3281                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3282                            {
3283                               if(sClass.templateClass) sClass = sClass.templateClass;
3284                               id += sClass.templateParams.count;
3285                            }
3286                            break;
3287                         }
3288                         id++;
3289                      }
3290                      if(curParam) break;
3291                   }
3292
3293                   if(curParam)
3294                   {
3295                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3296                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3297                   }
3298                }
3299
3300                // paramDest must be derived from paramSource
3301                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3302                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3303                {
3304                   char type[1024];
3305                   type[0] = 0;
3306                   PrintType(paramDest, type, false, true);
3307                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3308
3309                   if(paramDestType != paramDest)
3310                      FreeType(paramDestType);
3311                   return false;
3312                }
3313                if(paramDestType != paramDest)
3314                   FreeType(paramDestType);
3315             }
3316
3317             paramSource = paramSource.next;
3318          }
3319          if(paramSource)
3320          {
3321             Compiler_Error($"too many parameters\n");
3322             return false;
3323          }
3324          return true;
3325       }
3326       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3327       {
3328          return true;
3329       }
3330       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3331          (source.kind == arrayType || source.kind == pointerType))
3332       {
3333          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3334             return true;
3335       }
3336    }
3337    return false;
3338 }
3339
3340 static void FreeConvert(Conversion convert)
3341 {
3342    if(convert.resultType)
3343       FreeType(convert.resultType);
3344 }
3345
3346 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3347                               char * string, OldList conversions)
3348 {
3349    BTNamedLink link;
3350
3351    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3352    {
3353       Class _class = link.data;
3354       if(_class.type == enumClass)
3355       {
3356          OldList converts { };
3357          Type type { };
3358          type.kind = classType;
3359
3360          if(!_class.symbol)
3361             _class.symbol = FindClass(_class.fullName);
3362          type._class = _class.symbol;
3363
3364          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3365          {
3366             NamedLink value;
3367             Class enumClass = eSystem_FindClass(privateModule, "enum");
3368             if(enumClass)
3369             {
3370                Class baseClass;
3371                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3372                {
3373                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3374                   for(value = e.values.first; value; value = value.next)
3375                   {
3376                      if(!strcmp(value.name, string))
3377                         break;
3378                   }
3379                   if(value)
3380                   {
3381                      FreeExpContents(sourceExp);
3382                      FreeType(sourceExp.expType);
3383
3384                      sourceExp.isConstant = true;
3385                      sourceExp.expType = MkClassType(baseClass.fullName);
3386                      //if(inCompiler)
3387                      {
3388                         char constant[256];
3389                         sourceExp.type = constantExp;
3390                         if(!strcmp(baseClass.dataTypeString, "int"))
3391                            sprintf(constant, "%d",(int)value.data);
3392                         else
3393                            sprintf(constant, "0x%X",(int)value.data);
3394                         sourceExp.constant = CopyString(constant);
3395                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3396                      }
3397
3398                      while(converts.first)
3399                      {
3400                         Conversion convert = converts.first;
3401                         converts.Remove(convert);
3402                         conversions.Add(convert);
3403                      }
3404                      delete type;
3405                      return true;
3406                   }
3407                }
3408             }
3409          }
3410          if(converts.first)
3411             converts.Free(FreeConvert);
3412          delete type;
3413       }
3414    }
3415    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3416       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3417          return true;
3418    return false;
3419 }
3420
3421 public bool ModuleVisibility(Module searchIn, Module searchFor)
3422 {
3423    SubModule subModule;
3424
3425    if(searchFor == searchIn)
3426       return true;
3427
3428    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3429    {
3430       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3431       {
3432          if(ModuleVisibility(subModule.module, searchFor))
3433             return true;
3434       }
3435    }
3436    return false;
3437 }
3438
3439 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3440 {
3441    Module module;
3442
3443    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3444       return true;
3445    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3446       return true;
3447    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3448       return true;
3449
3450    for(module = mainModule.application.allModules.first; module; module = module.next)
3451    {
3452       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3453          return true;
3454    }
3455    return false;
3456 }
3457
3458 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3459 {
3460    Type source = sourceExp.expType;
3461    Type realDest = dest;
3462    Type backupSourceExpType = null;
3463
3464    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3465       return true;
3466
3467    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3468    {
3469        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3470        {
3471           Class sourceBase, destBase;
3472           for(sourceBase = source._class.registered;
3473               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3474               sourceBase = sourceBase.base);
3475           for(destBase = dest._class.registered;
3476               destBase && destBase.base && destBase.base.type != systemClass;
3477               destBase = destBase.base);
3478           //if(source._class.registered == dest._class.registered)
3479           if(sourceBase == destBase)
3480              return true;
3481        }
3482    }
3483
3484    if(source)
3485    {
3486       OldList * specs;
3487       bool flag = false;
3488       int64 value = MAXINT;
3489
3490       source.refCount++;
3491       dest.refCount++;
3492
3493       if(sourceExp.type == constantExp)
3494       {
3495          if(source.isSigned)
3496             value = strtoll(sourceExp.constant, null, 0);
3497          else
3498             value = strtoull(sourceExp.constant, null, 0);
3499       }
3500       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3501       {
3502          if(source.isSigned)
3503             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3504          else
3505             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3506       }
3507
3508       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3509          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3510       {
3511          FreeType(source);
3512          source = Type { kind = intType, isSigned = false, refCount = 1 };
3513       }
3514
3515       if(dest.kind == classType)
3516       {
3517          Class _class = dest._class ? dest._class.registered : null;
3518
3519          if(_class && _class.type == unitClass)
3520          {
3521             if(source.kind != classType)
3522             {
3523                Type tempType { };
3524                Type tempDest, tempSource;
3525
3526                for(; _class.base.type != systemClass; _class = _class.base);
3527                tempSource = dest;
3528                tempDest = tempType;
3529
3530                tempType.kind = classType;
3531                if(!_class.symbol)
3532                   _class.symbol = FindClass(_class.fullName);
3533
3534                tempType._class = _class.symbol;
3535                tempType.truth = dest.truth;
3536                if(tempType._class)
3537                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3538
3539                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3540                backupSourceExpType = sourceExp.expType;
3541                sourceExp.expType = dest; dest.refCount++;
3542                //sourceExp.expType = MkClassType(_class.fullName);
3543                flag = true;
3544
3545                delete tempType;
3546             }
3547          }
3548
3549
3550          // Why wasn't there something like this?
3551          if(_class && _class.type == bitClass && source.kind != classType)
3552          {
3553             if(!dest._class.registered.dataType)
3554                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3555             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3556             {
3557                FreeType(source);
3558                FreeType(sourceExp.expType);
3559                source = sourceExp.expType = MkClassType(dest._class.string);
3560                source.refCount++;
3561
3562                //source.kind = classType;
3563                //source._class = dest._class;
3564             }
3565          }
3566
3567          // Adding two enumerations
3568          /*
3569          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3570          {
3571             if(!source._class.registered.dataType)
3572                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3573             if(!dest._class.registered.dataType)
3574                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3575
3576             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3577             {
3578                FreeType(source);
3579                source = sourceExp.expType = MkClassType(dest._class.string);
3580                source.refCount++;
3581
3582                //source.kind = classType;
3583                //source._class = dest._class;
3584             }
3585          }*/
3586
3587          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3588          {
3589             OldList * specs = MkList();
3590             Declarator decl;
3591             char string[1024];
3592
3593             ReadString(string, sourceExp.string);
3594             decl = SpecDeclFromString(string, specs, null);
3595
3596             FreeExpContents(sourceExp);
3597             FreeType(sourceExp.expType);
3598
3599             sourceExp.type = classExp;
3600             sourceExp._classExp.specifiers = specs;
3601             sourceExp._classExp.decl = decl;
3602             sourceExp.expType = dest;
3603             dest.refCount++;
3604
3605             FreeType(source);
3606             FreeType(dest);
3607             if(backupSourceExpType) FreeType(backupSourceExpType);
3608             return true;
3609          }
3610       }
3611       else if(source.kind == classType)
3612       {
3613          Class _class = source._class ? source._class.registered : null;
3614
3615          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3616          {
3617             /*
3618             if(dest.kind != classType)
3619             {
3620                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3621                if(!source._class.registered.dataType)
3622                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3623
3624                FreeType(dest);
3625                dest = MkClassType(source._class.string);
3626                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3627                //   dest = MkClassType(source._class.string);
3628             }
3629             */
3630
3631             if(dest.kind != classType)
3632             {
3633                Type tempType { };
3634                Type tempDest, tempSource;
3635
3636                if(!source._class.registered.dataType)
3637                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3638
3639                for(; _class.base.type != systemClass; _class = _class.base);
3640                tempDest = source;
3641                tempSource = tempType;
3642                tempType.kind = classType;
3643                tempType._class = FindClass(_class.fullName);
3644                tempType.truth = source.truth;
3645                tempType.classObjectType = source.classObjectType;
3646
3647                if(tempType._class)
3648                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3649
3650                // PUT THIS BACK TESTING UNITS?
3651                if(conversions.last)
3652                {
3653                   ((Conversion)(conversions.last)).resultType = dest;
3654                   dest.refCount++;
3655                }
3656
3657                FreeType(sourceExp.expType);
3658                sourceExp.expType = MkClassType(_class.fullName);
3659                sourceExp.expType.truth = source.truth;
3660                sourceExp.expType.classObjectType = source.classObjectType;
3661
3662                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3663
3664                if(!sourceExp.destType)
3665                {
3666                   FreeType(sourceExp.destType);
3667                   sourceExp.destType = sourceExp.expType;
3668                   if(sourceExp.expType)
3669                      sourceExp.expType.refCount++;
3670                }
3671                //flag = true;
3672                //source = _class.dataType;
3673
3674
3675                // TOCHECK: TESTING THIS NEW CODE
3676                if(!_class.dataType)
3677                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3678                FreeType(dest);
3679                dest = MkClassType(source._class.string);
3680                dest.truth = source.truth;
3681                dest.classObjectType = source.classObjectType;
3682
3683                FreeType(source);
3684                source = _class.dataType;
3685                source.refCount++;
3686
3687                delete tempType;
3688             }
3689          }
3690       }
3691
3692       if(!flag)
3693       {
3694          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3695          {
3696             FreeType(source);
3697             FreeType(dest);
3698             return true;
3699          }
3700       }
3701
3702       // Implicit Casts
3703       /*
3704       if(source.kind == classType)
3705       {
3706          Class _class = source._class.registered;
3707          if(_class.type == unitClass)
3708          {
3709             if(!_class.dataType)
3710                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3711             source = _class.dataType;
3712          }
3713       }*/
3714
3715       if(dest.kind == classType)
3716       {
3717          Class _class = dest._class ? dest._class.registered : null;
3718          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") ||
3719             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3720          {
3721             if(_class.type == normalClass || _class.type == noHeadClass)
3722             {
3723                Expression newExp { };
3724                *newExp = *sourceExp;
3725                if(sourceExp.destType) sourceExp.destType.refCount++;
3726                if(sourceExp.expType)  sourceExp.expType.refCount++;
3727                sourceExp.type = castExp;
3728                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3729                sourceExp.cast.exp = newExp;
3730                FreeType(sourceExp.expType);
3731                sourceExp.expType = null;
3732                ProcessExpressionType(sourceExp);
3733
3734                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3735                if(!inCompiler)
3736                {
3737                   FreeType(sourceExp.expType);
3738                   sourceExp.expType = dest;
3739                }
3740
3741                FreeType(source);
3742                if(inCompiler) FreeType(dest);
3743
3744                if(backupSourceExpType) FreeType(backupSourceExpType);
3745                return true;
3746             }
3747
3748             if(!_class.dataType)
3749                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3750             FreeType(dest);
3751             dest = _class.dataType;
3752             dest.refCount++;
3753          }
3754
3755          // Accept lower precision types for units, since we want to keep the unit type
3756          if(dest.kind == doubleType &&
3757             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3758              source.kind == charType || source.kind == _BoolType))
3759          {
3760             specs = MkListOne(MkSpecifier(DOUBLE));
3761          }
3762          else if(dest.kind == floatType &&
3763             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3764             source.kind == _BoolType || source.kind == doubleType))
3765          {
3766             specs = MkListOne(MkSpecifier(FLOAT));
3767          }
3768          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3769             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3770          {
3771             specs = MkList();
3772             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3773             ListAdd(specs, MkSpecifier(INT64));
3774          }
3775          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3776             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3777          {
3778             specs = MkList();
3779             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3780             ListAdd(specs, MkSpecifier(INT));
3781          }
3782          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3783             source.kind == floatType || source.kind == doubleType))
3784          {
3785             specs = MkList();
3786             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3787             ListAdd(specs, MkSpecifier(SHORT));
3788          }
3789          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3790             source.kind == floatType || source.kind == doubleType))
3791          {
3792             specs = MkList();
3793             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3794             ListAdd(specs, MkSpecifier(CHAR));
3795          }
3796          else
3797          {
3798             FreeType(source);
3799             FreeType(dest);
3800             if(backupSourceExpType)
3801             {
3802                // Failed to convert: revert previous exp type
3803                if(sourceExp.expType) FreeType(sourceExp.expType);
3804                sourceExp.expType = backupSourceExpType;
3805             }
3806             return false;
3807          }
3808       }
3809       else if(dest.kind == doubleType &&
3810          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3811           source.kind == _BoolType || source.kind == charType))
3812       {
3813          specs = MkListOne(MkSpecifier(DOUBLE));
3814       }
3815       else if(dest.kind == floatType &&
3816          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3817       {
3818          specs = MkListOne(MkSpecifier(FLOAT));
3819       }
3820       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3821          (value == 1 || value == 0))
3822       {
3823          specs = MkList();
3824          ListAdd(specs, MkSpecifier(BOOL));
3825       }
3826       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3827          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3828       {
3829          specs = MkList();
3830          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3831          ListAdd(specs, MkSpecifier(CHAR));
3832       }
3833       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3834          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3835       {
3836          specs = MkList();
3837          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3838          ListAdd(specs, MkSpecifier(SHORT));
3839       }
3840       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3841       {
3842          specs = MkList();
3843          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3844          ListAdd(specs, MkSpecifier(INT));
3845       }
3846       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3847       {
3848          specs = MkList();
3849          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3850          ListAdd(specs, MkSpecifier(INT64));
3851       }
3852       else if(dest.kind == enumType &&
3853          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3854       {
3855          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3856       }
3857       else
3858       {
3859          FreeType(source);
3860          FreeType(dest);
3861          if(backupSourceExpType)
3862          {
3863             // Failed to convert: revert previous exp type
3864             if(sourceExp.expType) FreeType(sourceExp.expType);
3865             sourceExp.expType = backupSourceExpType;
3866          }
3867          return false;
3868       }
3869
3870       if(!flag)
3871       {
3872          Expression newExp { };
3873          *newExp = *sourceExp;
3874          newExp.prev = null;
3875          newExp.next = null;
3876          if(sourceExp.destType) sourceExp.destType.refCount++;
3877          if(sourceExp.expType)  sourceExp.expType.refCount++;
3878
3879          sourceExp.type = castExp;
3880          if(realDest.kind == classType)
3881          {
3882             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3883             FreeList(specs, FreeSpecifier);
3884          }
3885          else
3886             sourceExp.cast.typeName = MkTypeName(specs, null);
3887          if(newExp.type == opExp)
3888          {
3889             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3890          }
3891          else
3892             sourceExp.cast.exp = newExp;
3893
3894          FreeType(sourceExp.expType);
3895          sourceExp.expType = null;
3896          ProcessExpressionType(sourceExp);
3897       }
3898       else
3899          FreeList(specs, FreeSpecifier);
3900
3901       FreeType(dest);
3902       FreeType(source);
3903       if(backupSourceExpType) FreeType(backupSourceExpType);
3904
3905       return true;
3906    }
3907    else
3908    {
3909       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3910       if(sourceExp.type == identifierExp)
3911       {
3912          Identifier id = sourceExp.identifier;
3913          if(dest.kind == classType)
3914          {
3915             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3916             {
3917                Class _class = dest._class.registered;
3918                Class enumClass = eSystem_FindClass(privateModule, "enum");
3919                if(enumClass)
3920                {
3921                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3922                   {
3923                      NamedLink value;
3924                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3925                      for(value = e.values.first; value; value = value.next)
3926                      {
3927                         if(!strcmp(value.name, id.string))
3928                            break;
3929                      }
3930                      if(value)
3931                      {
3932                         FreeExpContents(sourceExp);
3933                         FreeType(sourceExp.expType);
3934
3935                         sourceExp.isConstant = true;
3936                         sourceExp.expType = MkClassType(_class.fullName);
3937                         //if(inCompiler)
3938                         {
3939                            char constant[256];
3940                            sourceExp.type = constantExp;
3941                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3942                               sprintf(constant, "%d", (int) value.data);
3943                            else
3944                               sprintf(constant, "0x%X", (int) value.data);
3945                            sourceExp.constant = CopyString(constant);
3946                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3947                         }
3948                         return true;
3949                      }
3950                   }
3951                }
3952             }
3953          }
3954
3955          // Loop through all enum classes
3956          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3957             return true;
3958       }
3959    }
3960    return false;
3961 }
3962
3963 #define TERTIARY(o, name, m, t, p) \
3964    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
3965    {                                                              \
3966       exp.type = constantExp;                                    \
3967       exp.string = p(op1.m ? op2.m : op3.m);                     \
3968       if(!exp.expType) \
3969          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3970       return true;                                                \
3971    }
3972
3973 #define BINARY(o, name, m, t, p) \
3974    static bool name(Expression exp, Operand op1, Operand op2)   \
3975    {                                                              \
3976       t value2 = op2.m;                                           \
3977       exp.type = constantExp;                                    \
3978       exp.string = p(op1.m o value2);                     \
3979       if(!exp.expType) \
3980          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3981       return true;                                                \
3982    }
3983
3984 #define BINARY_DIVIDE(o, name, m, t, p) \
3985    static bool name(Expression exp, Operand op1, Operand op2)   \
3986    {                                                              \
3987       t value2 = op2.m;                                           \
3988       exp.type = constantExp;                                    \
3989       exp.string = p(value2 ? (op1.m o value2) : 0);             \
3990       if(!exp.expType) \
3991          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3992       return true;                                                \
3993    }
3994
3995 #define UNARY(o, name, m, t, p) \
3996    static bool name(Expression exp, Operand op1)                \
3997    {                                                              \
3998       exp.type = constantExp;                                    \
3999       exp.string = p((t)(o op1.m));                                   \
4000       if(!exp.expType) \
4001          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4002       return true;                                                \
4003    }
4004
4005 #define OPERATOR_ALL(macro, o, name) \
4006    macro(o, Int##name, i, int, PrintInt) \
4007    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4008    macro(o, Int64##name, i64, int64, PrintInt64) \
4009    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4010    macro(o, Short##name, s, short, PrintShort) \
4011    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4012    macro(o, Char##name, c, char, PrintChar) \
4013    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4014    macro(o, Float##name, f, float, PrintFloat) \
4015    macro(o, Double##name, d, double, PrintDouble)
4016
4017 #define OPERATOR_INTTYPES(macro, o, name) \
4018    macro(o, Int##name, i, int, PrintInt) \
4019    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4020    macro(o, Int64##name, i64, int64, PrintInt64) \
4021    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4022    macro(o, Short##name, s, short, PrintShort) \
4023    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4024    macro(o, Char##name, c, char, PrintChar) \
4025    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4026
4027
4028 // binary arithmetic
4029 OPERATOR_ALL(BINARY, +, Add)
4030 OPERATOR_ALL(BINARY, -, Sub)
4031 OPERATOR_ALL(BINARY, *, Mul)
4032 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4033 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4034
4035 // unary arithmetic
4036 OPERATOR_ALL(UNARY, -, Neg)
4037
4038 // unary arithmetic increment and decrement
4039 OPERATOR_ALL(UNARY, ++, Inc)
4040 OPERATOR_ALL(UNARY, --, Dec)
4041
4042 // binary arithmetic assignment
4043 OPERATOR_ALL(BINARY, =, Asign)
4044 OPERATOR_ALL(BINARY, +=, AddAsign)
4045 OPERATOR_ALL(BINARY, -=, SubAsign)
4046 OPERATOR_ALL(BINARY, *=, MulAsign)
4047 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4048 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4049
4050 // binary bitwise
4051 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4052 OPERATOR_INTTYPES(BINARY, |, BitOr)
4053 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4054 OPERATOR_INTTYPES(BINARY, <<, LShift)
4055 OPERATOR_INTTYPES(BINARY, >>, RShift)
4056
4057 // unary bitwise
4058 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4059
4060 // binary bitwise assignment
4061 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4062 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4063 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4064 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4065 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4066
4067 // unary logical negation
4068 OPERATOR_INTTYPES(UNARY, !, Not)
4069
4070 // binary logical equality
4071 OPERATOR_ALL(BINARY, ==, Equ)
4072 OPERATOR_ALL(BINARY, !=, Nqu)
4073
4074 // binary logical
4075 OPERATOR_ALL(BINARY, &&, And)
4076 OPERATOR_ALL(BINARY, ||, Or)
4077
4078 // binary logical relational
4079 OPERATOR_ALL(BINARY, >, Grt)
4080 OPERATOR_ALL(BINARY, <, Sma)
4081 OPERATOR_ALL(BINARY, >=, GrtEqu)
4082 OPERATOR_ALL(BINARY, <=, SmaEqu)
4083
4084 // tertiary condition operator
4085 OPERATOR_ALL(TERTIARY, ?, Cond)
4086
4087 //Add, Sub, Mul, Div, Mod,     , Neg,     Inc, Dec,    Asign, AddAsign, SubAsign, MulAsign, DivAsign, ModAsign,     BitAnd, BitOr, BitXor, LShift, RShift, BitNot,     AndAsign, OrAsign, XorAsign, LShiftAsign, RShiftAsign,     Not,     Equ, Nqu,     And, Or,     Grt, Sma, GrtEqu, SmaEqu
4088 #define OPERATOR_TABLE_ALL(name, type) \
4089     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4090                           type##Neg, \
4091                           type##Inc, type##Dec, \
4092                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4093                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4094                           type##BitNot, \
4095                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4096                           type##Not, \
4097                           type##Equ, type##Nqu, \
4098                           type##And, type##Or, \
4099                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4100                         }; \
4101
4102 #define OPERATOR_TABLE_INTTYPES(name, type) \
4103     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4104                           type##Neg, \
4105                           type##Inc, type##Dec, \
4106                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4107                           null, null, null, null, null, \
4108                           null, \
4109                           null, null, null, null, null, \
4110                           null, \
4111                           type##Equ, type##Nqu, \
4112                           type##And, type##Or, \
4113                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4114                         }; \
4115
4116 OPERATOR_TABLE_ALL(int, Int)
4117 OPERATOR_TABLE_ALL(uint, UInt)
4118 OPERATOR_TABLE_ALL(int64, Int64)
4119 OPERATOR_TABLE_ALL(uint64, UInt64)
4120 OPERATOR_TABLE_ALL(short, Short)
4121 OPERATOR_TABLE_ALL(ushort, UShort)
4122 OPERATOR_TABLE_INTTYPES(float, Float)
4123 OPERATOR_TABLE_INTTYPES(double, Double)
4124 OPERATOR_TABLE_ALL(char, Char)
4125 OPERATOR_TABLE_ALL(uchar, UChar)
4126
4127 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4128 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4129 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4130 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4131 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4132 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4133 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4134 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4135
4136 public void ReadString(char * output,  char * string)
4137 {
4138    int len = strlen(string);
4139    int c,d = 0;
4140    bool quoted = false, escaped = false;
4141    for(c = 0; c<len; c++)
4142    {
4143       char ch = string[c];
4144       if(escaped)
4145       {
4146          switch(ch)
4147          {
4148             case 'n': output[d] = '\n'; break;
4149             case 't': output[d] = '\t'; break;
4150             case 'a': output[d] = '\a'; break;
4151             case 'b': output[d] = '\b'; break;
4152             case 'f': output[d] = '\f'; break;
4153             case 'r': output[d] = '\r'; break;
4154             case 'v': output[d] = '\v'; break;
4155             case '\\': output[d] = '\\'; break;
4156             case '\"': output[d] = '\"'; break;
4157             default: output[d++] = '\\'; output[d] = ch;
4158             //default: output[d] = ch;
4159          }
4160          d++;
4161          escaped = false;
4162       }
4163       else
4164       {
4165          if(ch == '\"')
4166             quoted ^= true;
4167          else if(quoted)
4168          {
4169             if(ch == '\\')
4170                escaped = true;
4171             else
4172                output[d++] = ch;
4173          }
4174       }
4175    }
4176    output[d] = '\0';
4177 }
4178
4179 public Operand GetOperand(Expression exp)
4180 {
4181    Operand op { };
4182    Type type = exp.expType;
4183    if(type)
4184    {
4185       while(type.kind == classType &&
4186          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4187       {
4188          if(!type._class.registered.dataType)
4189             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4190          type = type._class.registered.dataType;
4191
4192       }
4193       op.kind = type.kind;
4194       op.type = exp.expType;
4195       if(exp.type == stringExp && op.kind == pointerType)
4196       {
4197          op.ui64 = (uint64)exp.string;
4198          op.kind = pointerType;
4199          op.ops = uint64Ops;
4200       }
4201       else if(exp.isConstant && exp.type == constantExp)
4202       {
4203          switch(op.kind)
4204          {
4205             case _BoolType:
4206             case charType:
4207             {
4208                if(exp.constant[0] == '\'')
4209                   op.c = exp.constant[1];
4210                else if(type.isSigned)
4211                {
4212                   op.c = (char)strtol(exp.constant, null, 0);
4213                   op.ops = charOps;
4214                }
4215                else
4216                {
4217                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4218                   op.ops = ucharOps;
4219                }
4220                break;
4221             }
4222             case shortType:
4223                if(type.isSigned)
4224                {
4225                   op.s = (short)strtol(exp.constant, null, 0);
4226                   op.ops = shortOps;
4227                }
4228                else
4229                {
4230                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4231                   op.ops = ushortOps;
4232                }
4233                break;
4234             case intType:
4235             case longType:
4236                if(type.isSigned)
4237                {
4238                   op.i = (int)strtol(exp.constant, null, 0);
4239                   op.ops = intOps;
4240                }
4241                else
4242                {
4243                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4244                   op.ops = uintOps;
4245                }
4246                op.kind = intType;
4247                break;
4248             case int64Type:
4249                if(type.isSigned)
4250                {
4251                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4252                   op.ops = intOps;
4253                }
4254                else
4255                {
4256                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4257                   op.ops = uintOps;
4258                }
4259                op.kind = int64Type;
4260                break;
4261             case intPtrType:
4262                if(type.isSigned)
4263                {
4264                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4265                   op.ops = int64Ops;
4266                }
4267                else
4268                {
4269                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4270                   op.ops = uint64Ops;
4271                }
4272                op.kind = int64Type;
4273                break;
4274             case intSizeType:
4275                if(type.isSigned)
4276                {
4277                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4278                   op.ops = int64Ops;
4279                }
4280                else
4281                {
4282                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4283                   op.ops = uint64Ops;
4284                }
4285                op.kind = int64Type;
4286                break;
4287             case floatType:
4288                op.f = (float)strtod(exp.constant, null);
4289                op.ops = floatOps;
4290                break;
4291             case doubleType:
4292                op.d = (double)strtod(exp.constant, null);
4293                op.ops = doubleOps;
4294                break;
4295             //case classType:    For when we have operator overloading...
4296             // Pointer additions
4297             //case functionType:
4298             case arrayType:
4299             case pointerType:
4300             case classType:
4301                op.ui64 = _strtoui64(exp.constant, null, 0);
4302                op.kind = pointerType;
4303                op.ops = uint64Ops;
4304                // op.ptrSize =
4305                break;
4306          }
4307       }
4308    }
4309    return op;
4310 }
4311
4312 static void UnusedFunction()
4313 {
4314    int a;
4315    a.OnGetString(0,0,0);
4316 }
4317 default:
4318 extern int __ecereVMethodID_class_OnGetString;
4319 public:
4320
4321 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4322 {
4323    DataMember dataMember;
4324    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4325    {
4326       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4327          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4328       else
4329       {
4330          Expression exp { };
4331          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4332          Type type;
4333          void * ptr = inst.data + dataMember.offset + offset;
4334          char * result = null;
4335          exp.loc = member.loc = inst.loc;
4336          ((Identifier)member.identifiers->first).loc = inst.loc;
4337
4338          if(!dataMember.dataType)
4339             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4340          type = dataMember.dataType;
4341          if(type.kind == classType)
4342          {
4343             Class _class = type._class.registered;
4344             if(_class.type == enumClass)
4345             {
4346                Class enumClass = eSystem_FindClass(privateModule, "enum");
4347                if(enumClass)
4348                {
4349                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4350                   NamedLink item;
4351                   for(item = e.values.first; item; item = item.next)
4352                   {
4353                      if((int)item.data == *(int *)ptr)
4354                      {
4355                         result = item.name;
4356                         break;
4357                      }
4358                   }
4359                   if(result)
4360                   {
4361                      exp.identifier = MkIdentifier(result);
4362                      exp.type = identifierExp;
4363                      exp.destType = MkClassType(_class.fullName);
4364                      ProcessExpressionType(exp);
4365                   }
4366                }
4367             }
4368             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4369             {
4370                if(!_class.dataType)
4371                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4372                type = _class.dataType;
4373             }
4374          }
4375          if(!result)
4376          {
4377             switch(type.kind)
4378             {
4379                case floatType:
4380                {
4381                   FreeExpContents(exp);
4382
4383                   exp.constant = PrintFloat(*(float*)ptr);
4384                   exp.type = constantExp;
4385                   break;
4386                }
4387                case doubleType:
4388                {
4389                   FreeExpContents(exp);
4390
4391                   exp.constant = PrintDouble(*(double*)ptr);
4392                   exp.type = constantExp;
4393                   break;
4394                }
4395                case intType:
4396                {
4397                   FreeExpContents(exp);
4398
4399                   exp.constant = PrintInt(*(int*)ptr);
4400                   exp.type = constantExp;
4401                   break;
4402                }
4403                case int64Type:
4404                {
4405                   FreeExpContents(exp);
4406
4407                   exp.constant = PrintInt64(*(int64*)ptr);
4408                   exp.type = constantExp;
4409                   break;
4410                }
4411                case intPtrType:
4412                {
4413                   FreeExpContents(exp);
4414                   // TODO: This should probably use proper type
4415                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4416                   exp.type = constantExp;
4417                   break;
4418                }
4419                case intSizeType:
4420                {
4421                   FreeExpContents(exp);
4422                   // TODO: This should probably use proper type
4423                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4424                   exp.type = constantExp;
4425                   break;
4426                }
4427                default:
4428                   Compiler_Error($"Unhandled type populating instance\n");
4429             }
4430          }
4431          ListAdd(memberList, member);
4432       }
4433
4434       if(parentDataMember.type == unionMember)
4435          break;
4436    }
4437 }
4438
4439 void PopulateInstance(Instantiation inst)
4440 {
4441    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4442    Class _class = classSym.registered;
4443    DataMember dataMember;
4444    OldList * memberList = MkList();
4445    // Added this check and ->Add to prevent memory leaks on bad code
4446    if(!inst.members)
4447       inst.members = MkListOne(MkMembersInitList(memberList));
4448    else
4449       inst.members->Add(MkMembersInitList(memberList));
4450    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4451    {
4452       if(!dataMember.isProperty)
4453       {
4454          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4455             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4456          else
4457          {
4458             Expression exp { };
4459             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4460             Type type;
4461             void * ptr = inst.data + dataMember.offset;
4462             char * result = null;
4463
4464             exp.loc = member.loc = inst.loc;
4465             ((Identifier)member.identifiers->first).loc = inst.loc;
4466
4467             if(!dataMember.dataType)
4468                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4469             type = dataMember.dataType;
4470             if(type.kind == classType)
4471             {
4472                Class _class = type._class.registered;
4473                if(_class.type == enumClass)
4474                {
4475                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4476                   if(enumClass)
4477                   {
4478                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4479                      NamedLink item;
4480                      for(item = e.values.first; item; item = item.next)
4481                      {
4482                         if((int)item.data == *(int *)ptr)
4483                         {
4484                            result = item.name;
4485                            break;
4486                         }
4487                      }
4488                   }
4489                   if(result)
4490                   {
4491                      exp.identifier = MkIdentifier(result);
4492                      exp.type = identifierExp;
4493                      exp.destType = MkClassType(_class.fullName);
4494                      ProcessExpressionType(exp);
4495                   }
4496                }
4497                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4498                {
4499                   if(!_class.dataType)
4500                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4501                   type = _class.dataType;
4502                }
4503             }
4504             if(!result)
4505             {
4506                switch(type.kind)
4507                {
4508                   case floatType:
4509                   {
4510                      exp.constant = PrintFloat(*(float*)ptr);
4511                      exp.type = constantExp;
4512                      break;
4513                   }
4514                   case doubleType:
4515                   {
4516                      exp.constant = PrintDouble(*(double*)ptr);
4517                      exp.type = constantExp;
4518                      break;
4519                   }
4520                   case intType:
4521                   {
4522                      exp.constant = PrintInt(*(int*)ptr);
4523                      exp.type = constantExp;
4524                      break;
4525                   }
4526                   case int64Type:
4527                   {
4528                      exp.constant = PrintInt64(*(int64*)ptr);
4529                      exp.type = constantExp;
4530                      break;
4531                   }
4532                   case intPtrType:
4533                   {
4534                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4535                      exp.type = constantExp;
4536                      break;
4537                   }
4538                   default:
4539                      Compiler_Error($"Unhandled type populating instance\n");
4540                }
4541             }
4542             ListAdd(memberList, member);
4543          }
4544       }
4545    }
4546 }
4547
4548 void ComputeInstantiation(Expression exp)
4549 {
4550    Instantiation inst = exp.instance;
4551    MembersInit members;
4552    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4553    Class _class = classSym ? classSym.registered : null;
4554    DataMember curMember = null;
4555    Class curClass = null;
4556    DataMember subMemberStack[256];
4557    int subMemberStackPos = 0;
4558    uint64 bits = 0;
4559
4560    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4561    {
4562       // Don't recompute the instantiation...
4563       // Non Simple classes will have become constants by now
4564       if(inst.data)
4565          return;
4566
4567       if(_class.type == normalClass || _class.type == noHeadClass)
4568       {
4569          inst.data = (byte *)eInstance_New(_class);
4570          if(_class.type == normalClass)
4571             ((Instance)inst.data)._refCount++;
4572       }
4573       else
4574          inst.data = new0 byte[_class.structSize];
4575    }
4576
4577    if(inst.members)
4578    {
4579       for(members = inst.members->first; members; members = members.next)
4580       {
4581          switch(members.type)
4582          {
4583             case dataMembersInit:
4584             {
4585                if(members.dataMembers)
4586                {
4587                   MemberInit member;
4588                   for(member = members.dataMembers->first; member; member = member.next)
4589                   {
4590                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4591                      bool found = false;
4592
4593                      Property prop = null;
4594                      DataMember dataMember = null;
4595                      Method method = null;
4596                      uint dataMemberOffset;
4597
4598                      if(!ident)
4599                      {
4600                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4601                         if(curMember)
4602                         {
4603                            if(curMember.isProperty)
4604                               prop = (Property)curMember;
4605                            else
4606                            {
4607                               dataMember = curMember;
4608
4609                               // CHANGED THIS HERE
4610                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4611
4612                               // 2013/17/29 -- It seems that this was missing here!
4613                               if(_class.type == normalClass)
4614                                  dataMemberOffset += _class.base.structSize;
4615                               // dataMemberOffset = dataMember.offset;
4616                            }
4617                            found = true;
4618                         }
4619                      }
4620                      else
4621                      {
4622                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4623                         if(prop)
4624                         {
4625                            found = true;
4626                            if(prop.memberAccess == publicAccess)
4627                            {
4628                               curMember = (DataMember)prop;
4629                               curClass = prop._class;
4630                            }
4631                         }
4632                         else
4633                         {
4634                            DataMember _subMemberStack[256];
4635                            int _subMemberStackPos = 0;
4636
4637                            // FILL MEMBER STACK
4638                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4639
4640                            if(dataMember)
4641                            {
4642                               found = true;
4643                               if(dataMember.memberAccess == publicAccess)
4644                               {
4645                                  curMember = dataMember;
4646                                  curClass = dataMember._class;
4647                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4648                                  subMemberStackPos = _subMemberStackPos;
4649                               }
4650                            }
4651                         }
4652                      }
4653
4654                      if(found && member.initializer && member.initializer.type == expInitializer)
4655                      {
4656                         Expression value = member.initializer.exp;
4657                         Type type = null;
4658                         bool deepMember = false;
4659                         if(prop)
4660                         {
4661                            type = prop.dataType;
4662                         }
4663                         else if(dataMember)
4664                         {
4665                            if(!dataMember.dataType)
4666                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4667
4668                            type = dataMember.dataType;
4669                         }
4670
4671                         if(ident && ident.next)
4672                         {
4673                            deepMember = true;
4674
4675                            // for(; ident && type; ident = ident.next)
4676                            for(ident = ident.next; ident && type; ident = ident.next)
4677                            {
4678                               if(type.kind == classType)
4679                               {
4680                                  prop = eClass_FindProperty(type._class.registered,
4681                                     ident.string, privateModule);
4682                                  if(prop)
4683                                     type = prop.dataType;
4684                                  else
4685                                  {
4686                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4687                                        ident.string, &dataMemberOffset, privateModule, null, null);
4688                                     if(dataMember)
4689                                        type = dataMember.dataType;
4690                                  }
4691                               }
4692                               else if(type.kind == structType || type.kind == unionType)
4693                               {
4694                                  Type memberType;
4695                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4696                                  {
4697                                     if(!strcmp(memberType.name, ident.string))
4698                                     {
4699                                        type = memberType;
4700                                        break;
4701                                     }
4702                                  }
4703                               }
4704                            }
4705                         }
4706                         if(value)
4707                         {
4708                            FreeType(value.destType);
4709                            value.destType = type;
4710                            if(type) type.refCount++;
4711                            ComputeExpression(value);
4712                         }
4713                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4714                         {
4715                            if(type.kind == classType)
4716                            {
4717                               Class _class = type._class.registered;
4718                               if(_class.type == bitClass || _class.type == unitClass ||
4719                                  _class.type == enumClass)
4720                               {
4721                                  if(!_class.dataType)
4722                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4723                                  type = _class.dataType;
4724                               }
4725                            }
4726
4727                            if(dataMember)
4728                            {
4729                               void * ptr = inst.data + dataMemberOffset;
4730
4731                               if(value.type == constantExp)
4732                               {
4733                                  switch(type.kind)
4734                                  {
4735                                     case intType:
4736                                     {
4737                                        GetInt(value, (int*)ptr);
4738                                        break;
4739                                     }
4740                                     case int64Type:
4741                                     {
4742                                        GetInt64(value, (int64*)ptr);
4743                                        break;
4744                                     }
4745                                     case intPtrType:
4746                                     {
4747                                        GetIntPtr(value, (intptr*)ptr);
4748                                        break;
4749                                     }
4750                                     case intSizeType:
4751                                     {
4752                                        GetIntSize(value, (intsize*)ptr);
4753                                        break;
4754                                     }
4755                                     case floatType:
4756                                     {
4757                                        GetFloat(value, (float*)ptr);
4758                                        break;
4759                                     }
4760                                     case doubleType:
4761                                     {
4762                                        GetDouble(value, (double *)ptr);
4763                                        break;
4764                                     }
4765                                  }
4766                               }
4767                               else if(value.type == instanceExp)
4768                               {
4769                                  if(type.kind == classType)
4770                                  {
4771                                     Class _class = type._class.registered;
4772                                     if(_class.type == structClass)
4773                                     {
4774                                        ComputeTypeSize(type);
4775                                        if(value.instance.data)
4776                                           memcpy(ptr, value.instance.data, type.size);
4777                                     }
4778                                  }
4779                               }
4780                            }
4781                            else if(prop)
4782                            {
4783                               if(value.type == instanceExp && value.instance.data)
4784                               {
4785                                  if(type.kind == classType)
4786                                  {
4787                                     Class _class = type._class.registered;
4788                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4789                                     {
4790                                        void (*Set)(void *, void *) = (void *)prop.Set;
4791                                        Set(inst.data, value.instance.data);
4792                                        PopulateInstance(inst);
4793                                     }
4794                                  }
4795                               }
4796                               else if(value.type == constantExp)
4797                               {
4798                                  switch(type.kind)
4799                                  {
4800                                     case doubleType:
4801                                     {
4802                                        void (*Set)(void *, double) = (void *)prop.Set;
4803                                        Set(inst.data, strtod(value.constant, null) );
4804                                        break;
4805                                     }
4806                                     case floatType:
4807                                     {
4808                                        void (*Set)(void *, float) = (void *)prop.Set;
4809                                        Set(inst.data, (float)(strtod(value.constant, null)));
4810                                        break;
4811                                     }
4812                                     case intType:
4813                                     {
4814                                        void (*Set)(void *, int) = (void *)prop.Set;
4815                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4816                                        break;
4817                                     }
4818                                     case int64Type:
4819                                     {
4820                                        void (*Set)(void *, int64) = (void *)prop.Set;
4821                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4822                                        break;
4823                                     }
4824                                     case intPtrType:
4825                                     {
4826                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4827                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4828                                        break;
4829                                     }
4830                                     case intSizeType:
4831                                     {
4832                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4833                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4834                                        break;
4835                                     }
4836                                  }
4837                               }
4838                               else if(value.type == stringExp)
4839                               {
4840                                  char temp[1024];
4841                                  ReadString(temp, value.string);
4842                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4843                               }
4844                            }
4845                         }
4846                         else if(!deepMember && type && _class.type == unitClass)
4847                         {
4848                            if(prop)
4849                            {
4850                               // Only support converting units to units for now...
4851                               if(value.type == constantExp)
4852                               {
4853                                  if(type.kind == classType)
4854                                  {
4855                                     Class _class = type._class.registered;
4856                                     if(_class.type == unitClass)
4857                                     {
4858                                        if(!_class.dataType)
4859                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4860                                        type = _class.dataType;
4861                                     }
4862                                  }
4863                                  // TODO: Assuming same base type for units...
4864                                  switch(type.kind)
4865                                  {
4866                                     case floatType:
4867                                     {
4868                                        float fValue;
4869                                        float (*Set)(float) = (void *)prop.Set;
4870                                        GetFloat(member.initializer.exp, &fValue);
4871                                        exp.constant = PrintFloat(Set(fValue));
4872                                        exp.type = constantExp;
4873                                        break;
4874                                     }
4875                                     case doubleType:
4876                                     {
4877                                        double dValue;
4878                                        double (*Set)(double) = (void *)prop.Set;
4879                                        GetDouble(member.initializer.exp, &dValue);
4880                                        exp.constant = PrintDouble(Set(dValue));
4881                                        exp.type = constantExp;
4882                                        break;
4883                                     }
4884                                  }
4885                               }
4886                            }
4887                         }
4888                         else if(!deepMember && type && _class.type == bitClass)
4889                         {
4890                            if(prop)
4891                            {
4892                               if(value.type == instanceExp && value.instance.data)
4893                               {
4894                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4895                                  bits = Set(value.instance.data);
4896                               }
4897                               else if(value.type == constantExp)
4898                               {
4899                               }
4900                            }
4901                            else if(dataMember)
4902                            {
4903                               BitMember bitMember = (BitMember) dataMember;
4904                               Type type;
4905                               int part = 0;
4906                               GetInt(value, &part);
4907                               bits = (bits & ~bitMember.mask);
4908                               if(!bitMember.dataType)
4909                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
4910
4911                               type = bitMember.dataType;
4912
4913                               if(type.kind == classType && type._class && type._class.registered)
4914                               {
4915                                  if(!type._class.registered.dataType)
4916                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4917                                  type = type._class.registered.dataType;
4918                               }
4919
4920                               switch(type.kind)
4921                               {
4922                                  case _BoolType:
4923                                  case charType:
4924                                     if(type.isSigned)
4925                                        bits |= ((char)part << bitMember.pos);
4926                                     else
4927                                        bits |= ((unsigned char)part << bitMember.pos);
4928                                     break;
4929                                  case shortType:
4930                                     if(type.isSigned)
4931                                        bits |= ((short)part << bitMember.pos);
4932                                     else
4933                                        bits |= ((unsigned short)part << bitMember.pos);
4934                                     break;
4935                                  case intType:
4936                                  case longType:
4937                                     if(type.isSigned)
4938                                        bits |= ((int)part << bitMember.pos);
4939                                     else
4940                                        bits |= ((unsigned int)part << bitMember.pos);
4941                                     break;
4942                                  case int64Type:
4943                                     if(type.isSigned)
4944                                        bits |= ((int64)part << bitMember.pos);
4945                                     else
4946                                        bits |= ((uint64)part << bitMember.pos);
4947                                     break;
4948                                  case intPtrType:
4949                                     if(type.isSigned)
4950                                     {
4951                                        bits |= ((intptr)part << bitMember.pos);
4952                                     }
4953                                     else
4954                                     {
4955                                        bits |= ((uintptr)part << bitMember.pos);
4956                                     }
4957                                     break;
4958                                  case intSizeType:
4959                                     if(type.isSigned)
4960                                     {
4961                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
4962                                     }
4963                                     else
4964                                     {
4965                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
4966                                     }
4967                                     break;
4968                               }
4969                            }
4970                         }
4971                      }
4972                      else
4973                      {
4974                         if(_class && _class.type == unitClass)
4975                         {
4976                            ComputeExpression(member.initializer.exp);
4977                            exp.constant = member.initializer.exp.constant;
4978                            exp.type = constantExp;
4979
4980                            member.initializer.exp.constant = null;
4981                         }
4982                      }
4983                   }
4984                }
4985                break;
4986             }
4987          }
4988       }
4989    }
4990    if(_class && _class.type == bitClass)
4991    {
4992       exp.constant = PrintHexUInt(bits);
4993       exp.type = constantExp;
4994    }
4995    if(exp.type != instanceExp)
4996    {
4997       FreeInstance(inst);
4998    }
4999 }
5000
5001 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5002 {
5003    if(exp.op.op == SIZEOF)
5004    {
5005       FreeExpContents(exp);
5006       exp.type = constantExp;
5007       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5008    }
5009    else
5010    {
5011       if(!exp.op.exp1)
5012       {
5013          switch(exp.op.op)
5014          {
5015             // unary arithmetic
5016             case '+':
5017             {
5018                // Provide default unary +
5019                Expression exp2 = exp.op.exp2;
5020                exp.op.exp2 = null;
5021                FreeExpContents(exp);
5022                FreeType(exp.expType);
5023                FreeType(exp.destType);
5024                *exp = *exp2;
5025                delete exp2;
5026                break;
5027             }
5028             case '-':
5029                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5030                break;
5031             // unary arithmetic increment and decrement
5032                   //OPERATOR_ALL(UNARY, ++, Inc)
5033                   //OPERATOR_ALL(UNARY, --, Dec)
5034             // unary bitwise
5035             case '~':
5036                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5037                break;
5038             // unary logical negation
5039             case '!':
5040                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5041                break;
5042          }
5043       }
5044       else
5045       {
5046          switch(exp.op.op)
5047          {
5048             // binary arithmetic
5049             case '+':
5050                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5051                break;
5052             case '-':
5053                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5054                break;
5055             case '*':
5056                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5057                break;
5058             case '/':
5059                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5060                break;
5061             case '%':
5062                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5063                break;
5064             // binary arithmetic assignment
5065                   //OPERATOR_ALL(BINARY, =, Asign)
5066                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5067                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5068                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5069                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5070                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5071             // binary bitwise
5072             case '&':
5073                if(exp.op.exp2)
5074                {
5075                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5076                }
5077                break;
5078             case '|':
5079                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5080                break;
5081             case '^':
5082                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5083                break;
5084             case LEFT_OP:
5085                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5086                break;
5087             case RIGHT_OP:
5088                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5089                break;
5090             // binary bitwise assignment
5091                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5092                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5093                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5094                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5095                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5096             // binary logical equality
5097             case EQ_OP:
5098                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5099                break;
5100             case NE_OP:
5101                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5102                break;
5103             // binary logical
5104             case AND_OP:
5105                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5106                break;
5107             case OR_OP:
5108                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5109                break;
5110             // binary logical relational
5111             case '>':
5112                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5113                break;
5114             case '<':
5115                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5116                break;
5117             case GE_OP:
5118                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5119                break;
5120             case LE_OP:
5121                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5122                break;
5123          }
5124       }
5125    }
5126 }
5127
5128 void ComputeExpression(Expression exp)
5129 {
5130    char expString[10240];
5131    expString[0] = '\0';
5132 #ifdef _DEBUG
5133    PrintExpression(exp, expString);
5134 #endif
5135
5136    switch(exp.type)
5137    {
5138       case instanceExp:
5139       {
5140          ComputeInstantiation(exp);
5141          break;
5142       }
5143       /*
5144       case constantExp:
5145          break;
5146       */
5147       case opExp:
5148       {
5149          Expression exp1, exp2 = null;
5150          Operand op1 { };
5151          Operand op2 { };
5152
5153          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5154          if(exp.op.exp2)
5155             ComputeExpression(exp.op.exp2);
5156          if(exp.op.exp1)
5157          {
5158             ComputeExpression(exp.op.exp1);
5159             exp1 = exp.op.exp1;
5160             exp2 = exp.op.exp2;
5161             op1 = GetOperand(exp1);
5162             if(op1.type) op1.type.refCount++;
5163             if(exp2)
5164             {
5165                op2 = GetOperand(exp2);
5166                if(op2.type) op2.type.refCount++;
5167             }
5168          }
5169          else
5170          {
5171             exp1 = exp.op.exp2;
5172             op1 = GetOperand(exp1);
5173             if(op1.type) op1.type.refCount++;
5174          }
5175
5176          CallOperator(exp, exp1, exp2, op1, op2);
5177          /*
5178          switch(exp.op.op)
5179          {
5180             // Unary operators
5181             case '&':
5182                // Also binary
5183                if(exp.op.exp1 && exp.op.exp2)
5184                {
5185                   // Binary And
5186                   if(op1.ops.BitAnd)
5187                   {
5188                      FreeExpContents(exp);
5189                      op1.ops.BitAnd(exp, op1, op2);
5190                   }
5191                }
5192                break;
5193             case '*':
5194                if(exp.op.exp1)
5195                {
5196                   if(op1.ops.Mul)
5197                   {
5198                      FreeExpContents(exp);
5199                      op1.ops.Mul(exp, op1, op2);
5200                   }
5201                }
5202                break;
5203             case '+':
5204                if(exp.op.exp1)
5205                {
5206                   if(op1.ops.Add)
5207                   {
5208                      FreeExpContents(exp);
5209                      op1.ops.Add(exp, op1, op2);
5210                   }
5211                }
5212                else
5213                {
5214                   // Provide default unary +
5215                   Expression exp2 = exp.op.exp2;
5216                   exp.op.exp2 = null;
5217                   FreeExpContents(exp);
5218                   FreeType(exp.expType);
5219                   FreeType(exp.destType);
5220
5221                   *exp = *exp2;
5222                   delete exp2;
5223                }
5224                break;
5225             case '-':
5226                if(exp.op.exp1)
5227                {
5228                   if(op1.ops.Sub)
5229                   {
5230                      FreeExpContents(exp);
5231                      op1.ops.Sub(exp, op1, op2);
5232                   }
5233                }
5234                else
5235                {
5236                   if(op1.ops.Neg)
5237                   {
5238                      FreeExpContents(exp);
5239                      op1.ops.Neg(exp, op1);
5240                   }
5241                }
5242                break;
5243             case '~':
5244                if(op1.ops.BitNot)
5245                {
5246                   FreeExpContents(exp);
5247                   op1.ops.BitNot(exp, op1);
5248                }
5249                break;
5250             case '!':
5251                if(op1.ops.Not)
5252                {
5253                   FreeExpContents(exp);
5254                   op1.ops.Not(exp, op1);
5255                }
5256                break;
5257             // Binary only operators
5258             case '/':
5259                if(op1.ops.Div)
5260                {
5261                   FreeExpContents(exp);
5262                   op1.ops.Div(exp, op1, op2);
5263                }
5264                break;
5265             case '%':
5266                if(op1.ops.Mod)
5267                {
5268                   FreeExpContents(exp);
5269                   op1.ops.Mod(exp, op1, op2);
5270                }
5271                break;
5272             case LEFT_OP:
5273                break;
5274             case RIGHT_OP:
5275                break;
5276             case '<':
5277                if(exp.op.exp1)
5278                {
5279                   if(op1.ops.Sma)
5280                   {
5281                      FreeExpContents(exp);
5282                      op1.ops.Sma(exp, op1, op2);
5283                   }
5284                }
5285                break;
5286             case '>':
5287                if(exp.op.exp1)
5288                {
5289                   if(op1.ops.Grt)
5290                   {
5291                      FreeExpContents(exp);
5292                      op1.ops.Grt(exp, op1, op2);
5293                   }
5294                }
5295                break;
5296             case LE_OP:
5297                if(exp.op.exp1)
5298                {
5299                   if(op1.ops.SmaEqu)
5300                   {
5301                      FreeExpContents(exp);
5302                      op1.ops.SmaEqu(exp, op1, op2);
5303                   }
5304                }
5305                break;
5306             case GE_OP:
5307                if(exp.op.exp1)
5308                {
5309                   if(op1.ops.GrtEqu)
5310                   {
5311                      FreeExpContents(exp);
5312                      op1.ops.GrtEqu(exp, op1, op2);
5313                   }
5314                }
5315                break;
5316             case EQ_OP:
5317                if(exp.op.exp1)
5318                {
5319                   if(op1.ops.Equ)
5320                   {
5321                      FreeExpContents(exp);
5322                      op1.ops.Equ(exp, op1, op2);
5323                   }
5324                }
5325                break;
5326             case NE_OP:
5327                if(exp.op.exp1)
5328                {
5329                   if(op1.ops.Nqu)
5330                   {
5331                      FreeExpContents(exp);
5332                      op1.ops.Nqu(exp, op1, op2);
5333                   }
5334                }
5335                break;
5336             case '|':
5337                if(op1.ops.BitOr)
5338                {
5339                   FreeExpContents(exp);
5340                   op1.ops.BitOr(exp, op1, op2);
5341                }
5342                break;
5343             case '^':
5344                if(op1.ops.BitXor)
5345                {
5346                   FreeExpContents(exp);
5347                   op1.ops.BitXor(exp, op1, op2);
5348                }
5349                break;
5350             case AND_OP:
5351                break;
5352             case OR_OP:
5353                break;
5354             case SIZEOF:
5355                FreeExpContents(exp);
5356                exp.type = constantExp;
5357                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5358                break;
5359          }
5360          */
5361          if(op1.type) FreeType(op1.type);
5362          if(op2.type) FreeType(op2.type);
5363          break;
5364       }
5365       case bracketsExp:
5366       case extensionExpressionExp:
5367       {
5368          Expression e, n;
5369          for(e = exp.list->first; e; e = n)
5370          {
5371             n = e.next;
5372             if(!n)
5373             {
5374                OldList * list = exp.list;
5375                ComputeExpression(e);
5376                //FreeExpContents(exp);
5377                FreeType(exp.expType);
5378                FreeType(exp.destType);
5379                *exp = *e;
5380                delete e;
5381                delete list;
5382             }
5383             else
5384             {
5385                FreeExpression(e);
5386             }
5387          }
5388          break;
5389       }
5390       /*
5391
5392       case ExpIndex:
5393       {
5394          Expression e;
5395          exp.isConstant = true;
5396
5397          ComputeExpression(exp.index.exp);
5398          if(!exp.index.exp.isConstant)
5399             exp.isConstant = false;
5400
5401          for(e = exp.index.index->first; e; e = e.next)
5402          {
5403             ComputeExpression(e);
5404             if(!e.next)
5405             {
5406                // Check if this type is int
5407             }
5408             if(!e.isConstant)
5409                exp.isConstant = false;
5410          }
5411          exp.expType = Dereference(exp.index.exp.expType);
5412          break;
5413       }
5414       */
5415       case memberExp:
5416       {
5417          Expression memberExp = exp.member.exp;
5418          Identifier memberID = exp.member.member;
5419
5420          Type type;
5421          ComputeExpression(exp.member.exp);
5422          type = exp.member.exp.expType;
5423          if(type)
5424          {
5425             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);
5426             Property prop = null;
5427             DataMember member = null;
5428             Class convertTo = null;
5429             if(type.kind == subClassType && exp.member.exp.type == classExp)
5430                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5431
5432             if(!_class)
5433             {
5434                char string[256];
5435                Symbol classSym;
5436                string[0] = '\0';
5437                PrintTypeNoConst(type, string, false, true);
5438                classSym = FindClass(string);
5439                _class = classSym ? classSym.registered : null;
5440             }
5441
5442             if(exp.member.member)
5443             {
5444                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5445                if(!prop)
5446                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5447             }
5448             if(!prop && !member && _class && exp.member.member)
5449             {
5450                Symbol classSym = FindClass(exp.member.member.string);
5451                convertTo = _class;
5452                _class = classSym ? classSym.registered : null;
5453                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5454             }
5455
5456             if(prop)
5457             {
5458                if(prop.compiled)
5459                {
5460                   Type type = prop.dataType;
5461                   // TODO: Assuming same base type for units...
5462                   if(_class.type == unitClass)
5463                   {
5464                      if(type.kind == classType)
5465                      {
5466                         Class _class = type._class.registered;
5467                         if(_class.type == unitClass)
5468                         {
5469                            if(!_class.dataType)
5470                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5471                            type = _class.dataType;
5472                         }
5473                      }
5474                      switch(type.kind)
5475                      {
5476                         case floatType:
5477                         {
5478                            float value;
5479                            float (*Get)(float) = (void *)prop.Get;
5480                            GetFloat(exp.member.exp, &value);
5481                            exp.constant = PrintFloat(Get ? Get(value) : value);
5482                            exp.type = constantExp;
5483                            break;
5484                         }
5485                         case doubleType:
5486                         {
5487                            double value;
5488                            double (*Get)(double);
5489                            GetDouble(exp.member.exp, &value);
5490
5491                            if(convertTo)
5492                               Get = (void *)prop.Set;
5493                            else
5494                               Get = (void *)prop.Get;
5495                            exp.constant = PrintDouble(Get ? Get(value) : value);
5496                            exp.type = constantExp;
5497                            break;
5498                         }
5499                      }
5500                   }
5501                   else
5502                   {
5503                      if(convertTo)
5504                      {
5505                         Expression value = exp.member.exp;
5506                         Type type;
5507                         if(!prop.dataType)
5508                            ProcessPropertyType(prop);
5509
5510                         type = prop.dataType;
5511                         if(!type)
5512                         {
5513                             // printf("Investigate this\n");
5514                         }
5515                         else if(_class.type == structClass)
5516                         {
5517                            switch(type.kind)
5518                            {
5519                               case classType:
5520                               {
5521                                  Class propertyClass = type._class.registered;
5522                                  if(propertyClass.type == structClass && value.type == instanceExp)
5523                                  {
5524                                     void (*Set)(void *, void *) = (void *)prop.Set;
5525                                     exp.instance = Instantiation { };
5526                                     exp.instance.data = new0 byte[_class.structSize];
5527                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5528                                     exp.instance.loc = exp.loc;
5529                                     exp.type = instanceExp;
5530                                     Set(exp.instance.data, value.instance.data);
5531                                     PopulateInstance(exp.instance);
5532                                  }
5533                                  break;
5534                               }
5535                               case intType:
5536                               {
5537                                  int intValue;
5538                                  void (*Set)(void *, int) = (void *)prop.Set;
5539
5540                                  exp.instance = Instantiation { };
5541                                  exp.instance.data = new0 byte[_class.structSize];
5542                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5543                                  exp.instance.loc = exp.loc;
5544                                  exp.type = instanceExp;
5545
5546                                  GetInt(value, &intValue);
5547
5548                                  Set(exp.instance.data, intValue);
5549                                  PopulateInstance(exp.instance);
5550                                  break;
5551                               }
5552                               case int64Type:
5553                               {
5554                                  int64 intValue;
5555                                  void (*Set)(void *, int64) = (void *)prop.Set;
5556
5557                                  exp.instance = Instantiation { };
5558                                  exp.instance.data = new0 byte[_class.structSize];
5559                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5560                                  exp.instance.loc = exp.loc;
5561                                  exp.type = instanceExp;
5562
5563                                  GetInt64(value, &intValue);
5564
5565                                  Set(exp.instance.data, intValue);
5566                                  PopulateInstance(exp.instance);
5567                                  break;
5568                               }
5569                               case intPtrType:
5570                               {
5571                                  // TOFIX:
5572                                  intptr intValue;
5573                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5574
5575                                  exp.instance = Instantiation { };
5576                                  exp.instance.data = new0 byte[_class.structSize];
5577                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5578                                  exp.instance.loc = exp.loc;
5579                                  exp.type = instanceExp;
5580
5581                                  GetIntPtr(value, &intValue);
5582
5583                                  Set(exp.instance.data, intValue);
5584                                  PopulateInstance(exp.instance);
5585                                  break;
5586                               }
5587                               case intSizeType:
5588                               {
5589                                  // TOFIX:
5590                                  intsize intValue;
5591                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5592
5593                                  exp.instance = Instantiation { };
5594                                  exp.instance.data = new0 byte[_class.structSize];
5595                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5596                                  exp.instance.loc = exp.loc;
5597                                  exp.type = instanceExp;
5598
5599                                  GetIntSize(value, &intValue);
5600
5601                                  Set(exp.instance.data, intValue);
5602                                  PopulateInstance(exp.instance);
5603                                  break;
5604                               }
5605                               case doubleType:
5606                               {
5607                                  double doubleValue;
5608                                  void (*Set)(void *, double) = (void *)prop.Set;
5609
5610                                  exp.instance = Instantiation { };
5611                                  exp.instance.data = new0 byte[_class.structSize];
5612                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5613                                  exp.instance.loc = exp.loc;
5614                                  exp.type = instanceExp;
5615
5616                                  GetDouble(value, &doubleValue);
5617
5618                                  Set(exp.instance.data, doubleValue);
5619                                  PopulateInstance(exp.instance);
5620                                  break;
5621                               }
5622                            }
5623                         }
5624                         else if(_class.type == bitClass)
5625                         {
5626                            switch(type.kind)
5627                            {
5628                               case classType:
5629                               {
5630                                  Class propertyClass = type._class.registered;
5631                                  if(propertyClass.type == structClass && value.instance.data)
5632                                  {
5633                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5634                                     unsigned int bits = Set(value.instance.data);
5635                                     exp.constant = PrintHexUInt(bits);
5636                                     exp.type = constantExp;
5637                                     break;
5638                                  }
5639                                  else if(_class.type == bitClass)
5640                                  {
5641                                     unsigned int value;
5642                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5643                                     unsigned int bits;
5644
5645                                     GetUInt(exp.member.exp, &value);
5646                                     bits = Set(value);
5647                                     exp.constant = PrintHexUInt(bits);
5648                                     exp.type = constantExp;
5649                                  }
5650                               }
5651                            }
5652                         }
5653                      }
5654                      else
5655                      {
5656                         if(_class.type == bitClass)
5657                         {
5658                            unsigned int value;
5659                            GetUInt(exp.member.exp, &value);
5660
5661                            switch(type.kind)
5662                            {
5663                               case classType:
5664                               {
5665                                  Class _class = type._class.registered;
5666                                  if(_class.type == structClass)
5667                                  {
5668                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5669
5670                                     exp.instance = Instantiation { };
5671                                     exp.instance.data = new0 byte[_class.structSize];
5672                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5673                                     exp.instance.loc = exp.loc;
5674                                     //exp.instance.fullSet = true;
5675                                     exp.type = instanceExp;
5676                                     Get(value, exp.instance.data);
5677                                     PopulateInstance(exp.instance);
5678                                  }
5679                                  else if(_class.type == bitClass)
5680                                  {
5681                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5682                                     uint64 bits = Get(value);
5683                                     exp.constant = PrintHexUInt64(bits);
5684                                     exp.type = constantExp;
5685                                  }
5686                                  break;
5687                               }
5688                            }
5689                         }
5690                         else if(_class.type == structClass)
5691                         {
5692                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5693                            switch(type.kind)
5694                            {
5695                               case classType:
5696                               {
5697                                  Class _class = type._class.registered;
5698                                  if(_class.type == structClass && value)
5699                                  {
5700                                     void (*Get)(void *, void *) = (void *)prop.Get;
5701
5702                                     exp.instance = Instantiation { };
5703                                     exp.instance.data = new0 byte[_class.structSize];
5704                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5705                                     exp.instance.loc = exp.loc;
5706                                     //exp.instance.fullSet = true;
5707                                     exp.type = instanceExp;
5708                                     Get(value, exp.instance.data);
5709                                     PopulateInstance(exp.instance);
5710                                  }
5711                                  break;
5712                               }
5713                            }
5714                         }
5715                         /*else
5716                         {
5717                            char * value = exp.member.exp.instance.data;
5718                            switch(type.kind)
5719                            {
5720                               case classType:
5721                               {
5722                                  Class _class = type._class.registered;
5723                                  if(_class.type == normalClass)
5724                                  {
5725                                     void *(*Get)(void *) = (void *)prop.Get;
5726
5727                                     exp.instance = Instantiation { };
5728                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5729                                     exp.type = instanceExp;
5730                                     exp.instance.data = Get(value, exp.instance.data);
5731                                  }
5732                                  break;
5733                               }
5734                            }
5735                         }
5736                         */
5737                      }
5738                   }
5739                }
5740                else
5741                {
5742                   exp.isConstant = false;
5743                }
5744             }
5745             else if(member)
5746             {
5747             }
5748          }
5749
5750          if(exp.type != ExpressionType::memberExp)
5751          {
5752             FreeExpression(memberExp);
5753             FreeIdentifier(memberID);
5754          }
5755          break;
5756       }
5757       case typeSizeExp:
5758       {
5759          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5760          FreeExpContents(exp);
5761          exp.constant = PrintUInt(ComputeTypeSize(type));
5762          exp.type = constantExp;
5763          FreeType(type);
5764          break;
5765       }
5766       case classSizeExp:
5767       {
5768          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5769          if(classSym && classSym.registered)
5770          {
5771             if(classSym.registered.fixed)
5772             {
5773                FreeSpecifier(exp._class);
5774                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5775                exp.type = constantExp;
5776             }
5777             else
5778             {
5779                char className[1024];
5780                strcpy(className, "__ecereClass_");
5781                FullClassNameCat(className, classSym.string, true);
5782                MangleClassName(className);
5783
5784                DeclareClass(classSym, className);
5785
5786                FreeExpContents(exp);
5787                exp.type = pointerExp;
5788                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5789                exp.member.member = MkIdentifier("structSize");
5790             }
5791          }
5792          break;
5793       }
5794       case castExp:
5795       //case constantExp:
5796       {
5797          Type type;
5798          Expression e = exp;
5799          if(exp.type == castExp)
5800          {
5801             if(exp.cast.exp)
5802                ComputeExpression(exp.cast.exp);
5803             e = exp.cast.exp;
5804          }
5805          if(e && exp.expType)
5806          {
5807             /*if(exp.destType)
5808                type = exp.destType;
5809             else*/
5810                type = exp.expType;
5811             if(type.kind == classType)
5812             {
5813                Class _class = type._class.registered;
5814                if(_class && (_class.type == unitClass || _class.type == bitClass))
5815                {
5816                   if(!_class.dataType)
5817                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5818                   type = _class.dataType;
5819                }
5820             }
5821
5822             switch(type.kind)
5823             {
5824                case _BoolType:
5825                case charType:
5826                   if(type.isSigned)
5827                   {
5828                      char value;
5829                      GetChar(e, &value);
5830                      FreeExpContents(exp);
5831                      exp.constant = PrintChar(value);
5832                      exp.type = constantExp;
5833                   }
5834                   else
5835                   {
5836                      unsigned char value;
5837                      GetUChar(e, &value);
5838                      FreeExpContents(exp);
5839                      exp.constant = PrintUChar(value);
5840                      exp.type = constantExp;
5841                   }
5842                   break;
5843                case shortType:
5844                   if(type.isSigned)
5845                   {
5846                      short value;
5847                      GetShort(e, &value);
5848                      FreeExpContents(exp);
5849                      exp.constant = PrintShort(value);
5850                      exp.type = constantExp;
5851                   }
5852                   else
5853                   {
5854                      unsigned short value;
5855                      GetUShort(e, &value);
5856                      FreeExpContents(exp);
5857                      exp.constant = PrintUShort(value);
5858                      exp.type = constantExp;
5859                   }
5860                   break;
5861                case intType:
5862                   if(type.isSigned)
5863                   {
5864                      int value;
5865                      GetInt(e, &value);
5866                      FreeExpContents(exp);
5867                      exp.constant = PrintInt(value);
5868                      exp.type = constantExp;
5869                   }
5870                   else
5871                   {
5872                      unsigned int value;
5873                      GetUInt(e, &value);
5874                      FreeExpContents(exp);
5875                      exp.constant = PrintUInt(value);
5876                      exp.type = constantExp;
5877                   }
5878                   break;
5879                case int64Type:
5880                   if(type.isSigned)
5881                   {
5882                      int64 value;
5883                      GetInt64(e, &value);
5884                      FreeExpContents(exp);
5885                      exp.constant = PrintInt64(value);
5886                      exp.type = constantExp;
5887                   }
5888                   else
5889                   {
5890                      uint64 value;
5891                      GetUInt64(e, &value);
5892                      FreeExpContents(exp);
5893                      exp.constant = PrintUInt64(value);
5894                      exp.type = constantExp;
5895                   }
5896                   break;
5897                case intPtrType:
5898                   if(type.isSigned)
5899                   {
5900                      intptr value;
5901                      GetIntPtr(e, &value);
5902                      FreeExpContents(exp);
5903                      exp.constant = PrintInt64((int64)value);
5904                      exp.type = constantExp;
5905                   }
5906                   else
5907                   {
5908                      uintptr value;
5909                      GetUIntPtr(e, &value);
5910                      FreeExpContents(exp);
5911                      exp.constant = PrintUInt64((uint64)value);
5912                      exp.type = constantExp;
5913                   }
5914                   break;
5915                case intSizeType:
5916                   if(type.isSigned)
5917                   {
5918                      intsize value;
5919                      GetIntSize(e, &value);
5920                      FreeExpContents(exp);
5921                      exp.constant = PrintInt64((int64)value);
5922                      exp.type = constantExp;
5923                   }
5924                   else
5925                   {
5926                      uintsize value;
5927                      GetUIntSize(e, &value);
5928                      FreeExpContents(exp);
5929                      exp.constant = PrintUInt64((uint64)value);
5930                      exp.type = constantExp;
5931                   }
5932                   break;
5933                case floatType:
5934                {
5935                   float value;
5936                   GetFloat(e, &value);
5937                   FreeExpContents(exp);
5938                   exp.constant = PrintFloat(value);
5939                   exp.type = constantExp;
5940                   break;
5941                }
5942                case doubleType:
5943                {
5944                   double value;
5945                   GetDouble(e, &value);
5946                   FreeExpContents(exp);
5947                   exp.constant = PrintDouble(value);
5948                   exp.type = constantExp;
5949                   break;
5950                }
5951             }
5952          }
5953          break;
5954       }
5955       case conditionExp:
5956       {
5957          Operand op1 { };
5958          Operand op2 { };
5959          Operand op3 { };
5960
5961          if(exp.cond.exp)
5962             // Caring only about last expression for now...
5963             ComputeExpression(exp.cond.exp->last);
5964          if(exp.cond.elseExp)
5965             ComputeExpression(exp.cond.elseExp);
5966          if(exp.cond.cond)
5967             ComputeExpression(exp.cond.cond);
5968
5969          op1 = GetOperand(exp.cond.cond);
5970          if(op1.type) op1.type.refCount++;
5971          op2 = GetOperand(exp.cond.exp->last);
5972          if(op2.type) op2.type.refCount++;
5973          op3 = GetOperand(exp.cond.elseExp);
5974          if(op3.type) op3.type.refCount++;
5975
5976          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
5977          if(op1.type) FreeType(op1.type);
5978          if(op2.type) FreeType(op2.type);
5979          if(op3.type) FreeType(op3.type);
5980          break;
5981       }
5982    }
5983 }
5984
5985 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
5986 {
5987    bool result = true;
5988    if(destType)
5989    {
5990       OldList converts { };
5991       Conversion convert;
5992
5993       if(destType.kind == voidType)
5994          return false;
5995
5996       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
5997          result = false;
5998       if(converts.count)
5999       {
6000          // for(convert = converts.last; convert; convert = convert.prev)
6001          for(convert = converts.first; convert; convert = convert.next)
6002          {
6003             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6004             if(!empty)
6005             {
6006                Expression newExp { };
6007                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6008
6009                // TODO: Check this...
6010                *newExp = *exp;
6011                newExp.destType = null;
6012
6013                if(convert.isGet)
6014                {
6015                   // [exp].ColorRGB
6016                   exp.type = memberExp;
6017                   exp.addedThis = true;
6018                   exp.member.exp = newExp;
6019                   FreeType(exp.member.exp.expType);
6020
6021                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6022                   exp.member.exp.expType.classObjectType = objectType;
6023                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6024                   exp.member.memberType = propertyMember;
6025                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6026                   // TESTING THIS... for (int)degrees
6027                   exp.needCast = true;
6028                   if(exp.expType) exp.expType.refCount++;
6029                   ApplyAnyObjectLogic(exp.member.exp);
6030                }
6031                else
6032                {
6033
6034                   /*if(exp.isConstant)
6035                   {
6036                      // Color { ColorRGB = [exp] };
6037                      exp.type = instanceExp;
6038                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6039                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6040                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6041                   }
6042                   else*/
6043                   {
6044                      // If not constant, don't turn it yet into an instantiation
6045                      // (Go through the deep members system first)
6046                      exp.type = memberExp;
6047                      exp.addedThis = true;
6048                      exp.member.exp = newExp;
6049
6050                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6051                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6052                         newExp.expType._class.registered.type == noHeadClass)
6053                      {
6054                         newExp.byReference = true;
6055                      }
6056
6057                      FreeType(exp.member.exp.expType);
6058                      /*exp.member.exp.expType = convert.convert.dataType;
6059                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6060                      exp.member.exp.expType = null;
6061                      if(convert.convert.dataType)
6062                      {
6063                         exp.member.exp.expType = { };
6064                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6065                         exp.member.exp.expType.refCount = 1;
6066                         exp.member.exp.expType.classObjectType = objectType;
6067                         ApplyAnyObjectLogic(exp.member.exp);
6068                      }
6069
6070                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6071                      exp.member.memberType = reverseConversionMember;
6072                      exp.expType = convert.resultType ? convert.resultType :
6073                         MkClassType(convert.convert._class.fullName);
6074                      exp.needCast = true;
6075                      if(convert.resultType) convert.resultType.refCount++;
6076                   }
6077                }
6078             }
6079             else
6080             {
6081                FreeType(exp.expType);
6082                if(convert.isGet)
6083                {
6084                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6085                   exp.needCast = true;
6086                   if(exp.expType) exp.expType.refCount++;
6087                }
6088                else
6089                {
6090                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6091                   exp.needCast = true;
6092                   if(convert.resultType)
6093                      convert.resultType.refCount++;
6094                }
6095             }
6096          }
6097          if(exp.isConstant && inCompiler)
6098             ComputeExpression(exp);
6099
6100          converts.Free(FreeConvert);
6101       }
6102
6103       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6104       {
6105          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6106       }
6107       if(!result && exp.expType && exp.destType)
6108       {
6109          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6110              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6111             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6112             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6113             result = true;
6114       }
6115    }
6116    // if(result) CheckTemplateTypes(exp);
6117    return result;
6118 }
6119
6120 void CheckTemplateTypes(Expression exp)
6121 {
6122    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6123    {
6124       Expression newExp { };
6125       Statement compound;
6126       Context context;
6127       *newExp = *exp;
6128       if(exp.destType) exp.destType.refCount++;
6129       if(exp.expType)  exp.expType.refCount++;
6130       newExp.prev = null;
6131       newExp.next = null;
6132
6133       switch(exp.expType.kind)
6134       {
6135          case doubleType:
6136             if(exp.destType.classObjectType)
6137             {
6138                // We need to pass the address, just pass it along (Undo what was done above)
6139                if(exp.destType) exp.destType.refCount--;
6140                if(exp.expType)  exp.expType.refCount--;
6141                delete newExp;
6142             }
6143             else
6144             {
6145                // If we're looking for value:
6146                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6147                OldList * specs;
6148                OldList * unionDefs = MkList();
6149                OldList * statements = MkList();
6150                context = PushContext();
6151                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6152                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6153                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6154                exp.type = extensionCompoundExp;
6155                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6156                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6157                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6158                exp.compound.compound.context = context;
6159                PopContext(context);
6160             }
6161             break;
6162          default:
6163             exp.type = castExp;
6164             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6165             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6166             break;
6167       }
6168    }
6169    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6170    {
6171       Expression newExp { };
6172       Statement compound;
6173       Context context;
6174       *newExp = *exp;
6175       if(exp.destType) exp.destType.refCount++;
6176       if(exp.expType)  exp.expType.refCount++;
6177       newExp.prev = null;
6178       newExp.next = null;
6179
6180       switch(exp.expType.kind)
6181       {
6182          case doubleType:
6183             if(exp.destType.classObjectType)
6184             {
6185                // We need to pass the address, just pass it along (Undo what was done above)
6186                if(exp.destType) exp.destType.refCount--;
6187                if(exp.expType)  exp.expType.refCount--;
6188                delete newExp;
6189             }
6190             else
6191             {
6192                // If we're looking for value:
6193                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6194                OldList * specs;
6195                OldList * unionDefs = MkList();
6196                OldList * statements = MkList();
6197                context = PushContext();
6198                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6199                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6200                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6201                exp.type = extensionCompoundExp;
6202                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6203                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6204                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6205                exp.compound.compound.context = context;
6206                PopContext(context);
6207             }
6208             break;
6209          case classType:
6210          {
6211             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6212             {
6213                exp.type = bracketsExp;
6214                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6215                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6216                ProcessExpressionType(exp.list->first);
6217                break;
6218             }
6219             else
6220             {
6221                exp.type = bracketsExp;
6222                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6223                newExp.needCast = true;
6224                ProcessExpressionType(exp.list->first);
6225                break;
6226             }
6227          }
6228          default:
6229          {
6230             if(exp.expType.kind == templateType)
6231             {
6232                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6233                if(type)
6234                {
6235                   FreeType(exp.destType);
6236                   FreeType(exp.expType);
6237                   delete newExp;
6238                   break;
6239                }
6240             }
6241             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6242             {
6243                exp.type = opExp;
6244                exp.op.op = '*';
6245                exp.op.exp1 = null;
6246                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6247                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6248             }
6249             else
6250             {
6251                char typeString[1024];
6252                Declarator decl;
6253                OldList * specs = MkList();
6254                typeString[0] = '\0';
6255                PrintType(exp.expType, typeString, false, false);
6256                decl = SpecDeclFromString(typeString, specs, null);
6257
6258                exp.type = castExp;
6259                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6260                exp.cast.typeName = MkTypeName(specs, decl);
6261                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6262                exp.cast.exp.needCast = true;
6263             }
6264             break;
6265          }
6266       }
6267    }
6268 }
6269 // TODO: The Symbol tree should be reorganized by namespaces
6270 // Name Space:
6271 //    - Tree of all symbols within (stored without namespace)
6272 //    - Tree of sub-namespaces
6273
6274 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6275 {
6276    int nsLen = strlen(nameSpace);
6277    Symbol symbol;
6278    // Start at the name space prefix
6279    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6280    {
6281       char * s = symbol.string;
6282       if(!strncmp(s, nameSpace, nsLen))
6283       {
6284          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6285          int c;
6286          char * namePart;
6287          for(c = strlen(s)-1; c >= 0; c--)
6288             if(s[c] == ':')
6289                break;
6290
6291          namePart = s+c+1;
6292          if(!strcmp(namePart, name))
6293          {
6294             // TODO: Error on ambiguity
6295             return symbol;
6296          }
6297       }
6298       else
6299          break;
6300    }
6301    return null;
6302 }
6303
6304 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6305 {
6306    int c;
6307    char nameSpace[1024];
6308    char * namePart;
6309    bool gotColon = false;
6310
6311    nameSpace[0] = '\0';
6312    for(c = strlen(name)-1; c >= 0; c--)
6313       if(name[c] == ':')
6314       {
6315          gotColon = true;
6316          break;
6317       }
6318
6319    namePart = name+c+1;
6320    while(c >= 0 && name[c] == ':') c--;
6321    if(c >= 0)
6322    {
6323       // Try an exact match first
6324       Symbol symbol = (Symbol)tree.FindString(name);
6325       if(symbol)
6326          return symbol;
6327
6328       // Namespace specified
6329       memcpy(nameSpace, name, c + 1);
6330       nameSpace[c+1] = 0;
6331
6332       return ScanWithNameSpace(tree, nameSpace, namePart);
6333    }
6334    else if(gotColon)
6335    {
6336       // Looking for a global symbol, e.g. ::Sleep()
6337       Symbol symbol = (Symbol)tree.FindString(namePart);
6338       return symbol;
6339    }
6340    else
6341    {
6342       // Name only (no namespace specified)
6343       Symbol symbol = (Symbol)tree.FindString(namePart);
6344       if(symbol)
6345          return symbol;
6346       return ScanWithNameSpace(tree, "", namePart);
6347    }
6348    return null;
6349 }
6350
6351 static void ProcessDeclaration(Declaration decl);
6352
6353 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6354 {
6355 #ifdef _DEBUG
6356    //Time startTime = GetTime();
6357 #endif
6358    // Optimize this later? Do this before/less?
6359    Context ctx;
6360    Symbol symbol = null;
6361    // First, check if the identifier is declared inside the function
6362    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6363
6364    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6365    {
6366       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6367       {
6368          symbol = null;
6369          if(thisNameSpace)
6370          {
6371             char curName[1024];
6372             strcpy(curName, thisNameSpace);
6373             strcat(curName, "::");
6374             strcat(curName, name);
6375             // Try to resolve in current namespace first
6376             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6377          }
6378          if(!symbol)
6379             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6380       }
6381       else
6382          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6383
6384       if(symbol || ctx == endContext) break;
6385    }
6386    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6387    {
6388       if(symbol.pointerExternal.type == functionExternal)
6389       {
6390          FunctionDefinition function = symbol.pointerExternal.function;
6391
6392          // Modified this recently...
6393          Context tmpContext = curContext;
6394          curContext = null;
6395          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6396          curContext = tmpContext;
6397
6398          symbol.pointerExternal.symbol = symbol;
6399
6400          // TESTING THIS:
6401          DeclareType(symbol.type, true, true);
6402
6403          ast->Insert(curExternal.prev, symbol.pointerExternal);
6404
6405          symbol.id = curExternal.symbol.idCode;
6406
6407       }
6408       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6409       {
6410          ast->Move(symbol.pointerExternal, curExternal.prev);
6411          symbol.id = curExternal.symbol.idCode;
6412       }
6413    }
6414 #ifdef _DEBUG
6415    //findSymbolTotalTime += GetTime() - startTime;
6416 #endif
6417    return symbol;
6418 }
6419
6420 static void GetTypeSpecs(Type type, OldList * specs)
6421 {
6422    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6423    switch(type.kind)
6424    {
6425       case classType:
6426       {
6427          if(type._class.registered)
6428          {
6429             if(!type._class.registered.dataType)
6430                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6431             GetTypeSpecs(type._class.registered.dataType, specs);
6432          }
6433          break;
6434       }
6435       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6436       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6437       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6438       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6439       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6440       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6441       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6442       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6443       case intType:
6444       default:
6445          ListAdd(specs, MkSpecifier(INT)); break;
6446    }
6447 }
6448
6449 static void PrintArraySize(Type arrayType, char * string)
6450 {
6451    char size[256];
6452    size[0] = '\0';
6453    strcat(size, "[");
6454    if(arrayType.enumClass)
6455       strcat(size, arrayType.enumClass.string);
6456    else if(arrayType.arraySizeExp)
6457       PrintExpression(arrayType.arraySizeExp, size);
6458    strcat(size, "]");
6459    strcat(string, size);
6460 }
6461
6462 // WARNING : This function expects a null terminated string since it recursively concatenate...
6463 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6464 {
6465    if(type)
6466    {
6467       if(printConst && type.constant)
6468          strcat(string, "const ");
6469       switch(type.kind)
6470       {
6471          case classType:
6472          {
6473             Symbol c = type._class;
6474             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6475             //       look into merging with thisclass ?
6476             if(type.classObjectType == typedObject)
6477                strcat(string, "typed_object");
6478             else if(type.classObjectType == anyObject)
6479                strcat(string, "any_object");
6480             else
6481             {
6482                if(c && c.string)
6483                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6484             }
6485             if(type.byReference)
6486                strcat(string, " &");
6487             break;
6488          }
6489          case voidType: strcat(string, "void"); break;
6490          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6491          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6492          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6493          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6494          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6495          case _BoolType: strcat(string, "_Bool"); break;
6496          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6497          case floatType: strcat(string, "float"); break;
6498          case doubleType: strcat(string, "double"); break;
6499          case structType:
6500             if(type.enumName)
6501             {
6502                strcat(string, "struct ");
6503                strcat(string, type.enumName);
6504             }
6505             else if(type.typeName)
6506                strcat(string, type.typeName);
6507             else
6508             {
6509                Type member;
6510                strcat(string, "struct { ");
6511                for(member = type.members.first; member; member = member.next)
6512                {
6513                   PrintType(member, string, true, fullName);
6514                   strcat(string,"; ");
6515                }
6516                strcat(string,"}");
6517             }
6518             break;
6519          case unionType:
6520             if(type.enumName)
6521             {
6522                strcat(string, "union ");
6523                strcat(string, type.enumName);
6524             }
6525             else if(type.typeName)
6526                strcat(string, type.typeName);
6527             else
6528             {
6529                strcat(string, "union ");
6530                strcat(string,"(unnamed)");
6531             }
6532             break;
6533          case enumType:
6534             if(type.enumName)
6535             {
6536                strcat(string, "enum ");
6537                strcat(string, type.enumName);
6538             }
6539             else if(type.typeName)
6540                strcat(string, type.typeName);
6541             else
6542                strcat(string, "int"); // "enum");
6543             break;
6544          case ellipsisType:
6545             strcat(string, "...");
6546             break;
6547          case subClassType:
6548             strcat(string, "subclass(");
6549             strcat(string, type._class ? type._class.string : "int");
6550             strcat(string, ")");
6551             break;
6552          case templateType:
6553             strcat(string, type.templateParameter.identifier.string);
6554             break;
6555          case thisClassType:
6556             strcat(string, "thisclass");
6557             break;
6558          case vaListType:
6559             strcat(string, "__builtin_va_list");
6560             break;
6561       }
6562    }
6563 }
6564
6565 static void PrintName(Type type, char * string, bool fullName)
6566 {
6567    if(type.name && type.name[0])
6568    {
6569       if(fullName)
6570          strcat(string, type.name);
6571       else
6572       {
6573          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6574          if(name) name += 2; else name = type.name;
6575          strcat(string, name);
6576       }
6577    }
6578 }
6579
6580 static void PrintAttribs(Type type, char * string)
6581 {
6582    if(type)
6583    {
6584       if(type.dllExport)   strcat(string, "dllexport ");
6585       if(type.attrStdcall) strcat(string, "stdcall ");
6586    }
6587 }
6588
6589 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6590 {
6591    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6592    {
6593       Type attrType = null;
6594       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6595          PrintAttribs(type, string);
6596       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6597          strcat(string, " const");
6598       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6599       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6600          strcat(string, " (");
6601       if(type.kind == pointerType)
6602       {
6603          if(type.type.kind == functionType || type.type.kind == methodType)
6604             PrintAttribs(type.type, string);
6605       }
6606       if(type.kind == pointerType)
6607       {
6608          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6609             strcat(string, "*");
6610          else
6611             strcat(string, " *");
6612       }
6613       if(printConst && type.constant && type.kind == pointerType)
6614          strcat(string, " const");
6615    }
6616    else
6617       PrintTypeSpecs(type, string, fullName, printConst);
6618 }
6619
6620 static void PostPrintType(Type type, char * string, bool fullName)
6621 {
6622    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6623       strcat(string, ")");
6624    if(type.kind == arrayType)
6625       PrintArraySize(type, string);
6626    else if(type.kind == functionType)
6627    {
6628       Type param;
6629       strcat(string, "(");
6630       for(param = type.params.first; param; param = param.next)
6631       {
6632          PrintType(param, string, true, fullName);
6633          if(param.next) strcat(string, ", ");
6634       }
6635       strcat(string, ")");
6636    }
6637    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6638       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6639 }
6640
6641 // *****
6642 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6643 // *****
6644 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6645 {
6646    PrePrintType(type, string, fullName, null, printConst);
6647
6648    if(type.thisClass || (printName && type.name && type.name[0]))
6649       strcat(string, " ");
6650    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6651    {
6652       Symbol _class = type.thisClass;
6653       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6654       {
6655          if(type.classObjectType == classPointer)
6656             strcat(string, "class");
6657          else
6658             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6659       }
6660       else if(_class && _class.string)
6661       {
6662          String s = _class.string;
6663          if(fullName)
6664             strcat(string, s);
6665          else
6666          {
6667             char * name = RSearchString(s, "::", strlen(s), true, false);
6668             if(name) name += 2; else name = s;
6669             strcat(string, name);
6670          }
6671       }
6672       strcat(string, "::");
6673    }
6674
6675    if(printName && type.name)
6676       PrintName(type, string, fullName);
6677    PostPrintType(type, string, fullName);
6678    if(type.bitFieldCount)
6679    {
6680       char count[100];
6681       sprintf(count, ":%d", type.bitFieldCount);
6682       strcat(string, count);
6683    }
6684 }
6685
6686 void PrintType(Type type, char * string, bool printName, bool fullName)
6687 {
6688    _PrintType(type, string, printName, fullName, true);
6689 }
6690
6691 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6692 {
6693    _PrintType(type, string, printName, fullName, false);
6694 }
6695
6696 static Type FindMember(Type type, char * string)
6697 {
6698    Type memberType;
6699    for(memberType = type.members.first; memberType; memberType = memberType.next)
6700    {
6701       if(!memberType.name)
6702       {
6703          Type subType = FindMember(memberType, string);
6704          if(subType)
6705             return subType;
6706       }
6707       else if(!strcmp(memberType.name, string))
6708          return memberType;
6709    }
6710    return null;
6711 }
6712
6713 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6714 {
6715    Type memberType;
6716    for(memberType = type.members.first; memberType; memberType = memberType.next)
6717    {
6718       if(!memberType.name)
6719       {
6720          Type subType = FindMember(memberType, string);
6721          if(subType)
6722          {
6723             *offset += memberType.offset;
6724             return subType;
6725          }
6726       }
6727       else if(!strcmp(memberType.name, string))
6728       {
6729          *offset += memberType.offset;
6730          return memberType;
6731       }
6732    }
6733    return null;
6734 }
6735
6736 public bool GetParseError() { return parseError; }
6737
6738 Expression ParseExpressionString(char * expression)
6739 {
6740    parseError = false;
6741
6742    fileInput = TempFile { };
6743    fileInput.Write(expression, 1, strlen(expression));
6744    fileInput.Seek(0, start);
6745
6746    echoOn = false;
6747    parsedExpression = null;
6748    resetScanner();
6749    expression_yyparse();
6750    delete fileInput;
6751
6752    return parsedExpression;
6753 }
6754
6755 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6756 {
6757    Identifier id = exp.identifier;
6758    Method method = null;
6759    Property prop = null;
6760    DataMember member = null;
6761    ClassProperty classProp = null;
6762
6763    if(_class && _class.type == enumClass)
6764    {
6765       NamedLink value = null;
6766       Class enumClass = eSystem_FindClass(privateModule, "enum");
6767       if(enumClass)
6768       {
6769          Class baseClass;
6770          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6771          {
6772             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6773             for(value = e.values.first; value; value = value.next)
6774             {
6775                if(!strcmp(value.name, id.string))
6776                   break;
6777             }
6778             if(value)
6779             {
6780                char constant[256];
6781
6782                FreeExpContents(exp);
6783
6784                exp.type = constantExp;
6785                exp.isConstant = true;
6786                if(!strcmp(baseClass.dataTypeString, "int"))
6787                   sprintf(constant, "%d",(int)value.data);
6788                else
6789                   sprintf(constant, "0x%X",(int)value.data);
6790                exp.constant = CopyString(constant);
6791                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
6792                exp.expType = MkClassType(baseClass.fullName);
6793                break;
6794             }
6795          }
6796       }
6797       if(value)
6798          return true;
6799    }
6800    if((method = eClass_FindMethod(_class, id.string, privateModule)))
6801    {
6802       ProcessMethodType(method);
6803       exp.expType = Type
6804       {
6805          refCount = 1;
6806          kind = methodType;
6807          method = method;
6808          // Crash here?
6809          // TOCHECK: Put it back to what it was...
6810          // methodClass = _class;
6811          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
6812       };
6813       //id._class = null;
6814       return true;
6815    }
6816    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
6817    {
6818       if(!prop.dataType)
6819          ProcessPropertyType(prop);
6820       exp.expType = prop.dataType;
6821       if(prop.dataType) prop.dataType.refCount++;
6822       return true;
6823    }
6824    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
6825    {
6826       if(!member.dataType)
6827          member.dataType = ProcessTypeString(member.dataTypeString, false);
6828       exp.expType = member.dataType;
6829       if(member.dataType) member.dataType.refCount++;
6830       return true;
6831    }
6832    else if((classProp = eClass_FindClassProperty(_class, id.string)))
6833    {
6834       if(!classProp.dataType)
6835          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
6836
6837       if(classProp.constant)
6838       {
6839          FreeExpContents(exp);
6840
6841          exp.isConstant = true;
6842          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
6843          {
6844             //char constant[256];
6845             exp.type = stringExp;
6846             exp.constant = QMkString((char *)classProp.Get(_class));
6847          }
6848          else
6849          {
6850             char constant[256];
6851             exp.type = constantExp;
6852             sprintf(constant, "%d", (int)classProp.Get(_class));
6853             exp.constant = CopyString(constant);
6854          }
6855       }
6856       else
6857       {
6858          // TO IMPLEMENT...
6859       }
6860
6861       exp.expType = classProp.dataType;
6862       if(classProp.dataType) classProp.dataType.refCount++;
6863       return true;
6864    }
6865    return false;
6866 }
6867
6868 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
6869 {
6870    BinaryTree * tree = &nameSpace.functions;
6871    GlobalData data = (GlobalData)tree->FindString(name);
6872    NameSpace * child;
6873    if(!data)
6874    {
6875       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
6876       {
6877          data = ScanGlobalData(child, name);
6878          if(data)
6879             break;
6880       }
6881    }
6882    return data;
6883 }
6884
6885 static GlobalData FindGlobalData(char * name)
6886 {
6887    int start = 0, c;
6888    NameSpace * nameSpace;
6889    nameSpace = globalData;
6890    for(c = 0; name[c]; c++)
6891    {
6892       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
6893       {
6894          NameSpace * newSpace;
6895          char * spaceName = new char[c - start + 1];
6896          strncpy(spaceName, name + start, c - start);
6897          spaceName[c-start] = '\0';
6898          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
6899          delete spaceName;
6900          if(!newSpace)
6901             return null;
6902          nameSpace = newSpace;
6903          if(name[c] == ':') c++;
6904          start = c+1;
6905       }
6906    }
6907    if(c - start)
6908    {
6909       return ScanGlobalData(nameSpace, name + start);
6910    }
6911    return null;
6912 }
6913
6914 static int definedExpStackPos;
6915 static void * definedExpStack[512];
6916
6917 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
6918 void ReplaceExpContents(Expression checkedExp, Expression newExp)
6919 {
6920    Expression prev = checkedExp.prev, next = checkedExp.next;
6921
6922    FreeExpContents(checkedExp);
6923    FreeType(checkedExp.expType);
6924    FreeType(checkedExp.destType);
6925
6926    *checkedExp = *newExp;
6927
6928    delete newExp;
6929
6930    checkedExp.prev = prev;
6931    checkedExp.next = next;
6932 }
6933
6934 void ApplyAnyObjectLogic(Expression e)
6935 {
6936    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
6937 #ifdef _DEBUG
6938    char debugExpString[4096];
6939    debugExpString[0] = '\0';
6940    PrintExpression(e, debugExpString);
6941 #endif
6942
6943    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
6944    {
6945       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
6946       //ellipsisDestType = destType;
6947       if(e && e.expType)
6948       {
6949          Type type = e.expType;
6950          Class _class = null;
6951          //Type destType = e.destType;
6952
6953          if(type.kind == classType && type._class && type._class.registered)
6954          {
6955             _class = type._class.registered;
6956          }
6957          else if(type.kind == subClassType)
6958          {
6959             _class = FindClass("ecere::com::Class").registered;
6960          }
6961          else
6962          {
6963             char string[1024] = "";
6964             Symbol classSym;
6965
6966             PrintTypeNoConst(type, string, false, true);
6967             classSym = FindClass(string);
6968             if(classSym) _class = classSym.registered;
6969          }
6970
6971          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...
6972             (!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))) ||
6973             destType.byReference)))
6974          {
6975             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
6976             {
6977                Expression checkedExp = e, newExp;
6978
6979                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
6980                {
6981                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
6982                   {
6983                      if(checkedExp.type == extensionCompoundExp)
6984                      {
6985                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
6986                      }
6987                      else
6988                         checkedExp = checkedExp.list->last;
6989                   }
6990                   else if(checkedExp.type == castExp)
6991                      checkedExp = checkedExp.cast.exp;
6992                }
6993
6994                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
6995                {
6996                   newExp = checkedExp.op.exp2;
6997                   checkedExp.op.exp2 = null;
6998                   FreeExpContents(checkedExp);
6999
7000                   if(e.expType && e.expType.passAsTemplate)
7001                   {
7002                      char size[100];
7003                      ComputeTypeSize(e.expType);
7004                      sprintf(size, "%d", e.expType.size);
7005                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7006                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7007                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7008                   }
7009
7010                   ReplaceExpContents(checkedExp, newExp);
7011                   e.byReference = true;
7012                }
7013                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7014                {
7015                   Expression checkedExp, newExp;
7016
7017                   {
7018                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7019                      bool hasAddress =
7020                         e.type == identifierExp ||
7021                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7022                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7023                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7024                         e.type == indexExp;
7025
7026                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7027                      {
7028                         Context context = PushContext();
7029                         Declarator decl;
7030                         OldList * specs = MkList();
7031                         char typeString[1024];
7032                         Expression newExp { };
7033
7034                         typeString[0] = '\0';
7035                         *newExp = *e;
7036
7037                         //if(e.destType) e.destType.refCount++;
7038                         // if(exp.expType) exp.expType.refCount++;
7039                         newExp.prev = null;
7040                         newExp.next = null;
7041                         newExp.expType = null;
7042
7043                         PrintTypeNoConst(e.expType, typeString, false, true);
7044                         decl = SpecDeclFromString(typeString, specs, null);
7045                         newExp.destType = ProcessType(specs, decl);
7046
7047                         curContext = context;
7048
7049                         // We need a current compound for this
7050                         if(curCompound)
7051                         {
7052                            char name[100];
7053                            OldList * stmts = MkList();
7054                            e.type = extensionCompoundExp;
7055                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7056                            if(!curCompound.compound.declarations)
7057                               curCompound.compound.declarations = MkList();
7058                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7059                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7060                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7061                            e.compound = MkCompoundStmt(null, stmts);
7062                         }
7063                         else
7064                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7065
7066                         /*
7067                         e.compound = MkCompoundStmt(
7068                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7069                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7070
7071                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7072                         */
7073
7074                         {
7075                            Type type = e.destType;
7076                            e.destType = { };
7077                            CopyTypeInto(e.destType, type);
7078                            e.destType.refCount = 1;
7079                            e.destType.classObjectType = none;
7080                            FreeType(type);
7081                         }
7082
7083                         e.compound.compound.context = context;
7084                         PopContext(context);
7085                         curContext = context.parent;
7086                      }
7087                   }
7088
7089                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7090                   checkedExp = e;
7091                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7092                   {
7093                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7094                      {
7095                         if(checkedExp.type == extensionCompoundExp)
7096                         {
7097                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7098                         }
7099                         else
7100                            checkedExp = checkedExp.list->last;
7101                      }
7102                      else if(checkedExp.type == castExp)
7103                         checkedExp = checkedExp.cast.exp;
7104                   }
7105                   {
7106                      Expression operand { };
7107                      operand = *checkedExp;
7108                      checkedExp.destType = null;
7109                      checkedExp.expType = null;
7110                      checkedExp.Clear();
7111                      checkedExp.type = opExp;
7112                      checkedExp.op.op = '&';
7113                      checkedExp.op.exp1 = null;
7114                      checkedExp.op.exp2 = operand;
7115
7116                      //newExp = MkExpOp(null, '&', checkedExp);
7117                   }
7118                   //ReplaceExpContents(checkedExp, newExp);
7119                }
7120             }
7121          }
7122       }
7123    }
7124    {
7125       // If expression type is a simple class, make it an address
7126       // FixReference(e, true);
7127    }
7128 //#if 0
7129    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7130       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7131          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7132    {
7133       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"))
7134       {
7135          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7136       }
7137       else
7138       {
7139          Expression thisExp { };
7140
7141          *thisExp = *e;
7142          thisExp.prev = null;
7143          thisExp.next = null;
7144          e.Clear();
7145
7146          e.type = bracketsExp;
7147          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7148          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7149             ((Expression)e.list->first).byReference = true;
7150
7151          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7152          {
7153             e.expType = thisExp.expType;
7154             e.expType.refCount++;
7155          }
7156          else*/
7157          {
7158             e.expType = { };
7159             CopyTypeInto(e.expType, thisExp.expType);
7160             e.expType.byReference = false;
7161             e.expType.refCount = 1;
7162
7163             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7164                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7165             {
7166                e.expType.classObjectType = none;
7167             }
7168          }
7169       }
7170    }
7171 // TOFIX: Try this for a nice IDE crash!
7172 //#endif
7173    // The other way around
7174    else
7175 //#endif
7176    if(destType && e.expType &&
7177          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7178          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7179          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7180    {
7181       if(destType.kind == ellipsisType)
7182       {
7183          Compiler_Error($"Unspecified type\n");
7184       }
7185       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7186       {
7187          bool byReference = e.expType.byReference;
7188          Expression thisExp { };
7189          Declarator decl;
7190          OldList * specs = MkList();
7191          char typeString[1024]; // Watch buffer overruns
7192          Type type;
7193          ClassObjectType backupClassObjectType;
7194          bool backupByReference;
7195
7196          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7197             type = e.expType;
7198          else
7199             type = destType;
7200
7201          backupClassObjectType = type.classObjectType;
7202          backupByReference = type.byReference;
7203
7204          type.classObjectType = none;
7205          type.byReference = false;
7206
7207          typeString[0] = '\0';
7208          PrintType(type, typeString, false, true);
7209          decl = SpecDeclFromString(typeString, specs, null);
7210
7211          type.classObjectType = backupClassObjectType;
7212          type.byReference = backupByReference;
7213
7214          *thisExp = *e;
7215          thisExp.prev = null;
7216          thisExp.next = null;
7217          e.Clear();
7218
7219          if( ( type.kind == classType && type._class && type._class.registered &&
7220                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7221                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7222              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7223              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7224          {
7225             e.type = opExp;
7226             e.op.op = '*';
7227             e.op.exp1 = null;
7228             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7229
7230             e.expType = { };
7231             CopyTypeInto(e.expType, type);
7232             e.expType.byReference = false;
7233             e.expType.refCount = 1;
7234          }
7235          else
7236          {
7237             e.type = castExp;
7238             e.cast.typeName = MkTypeName(specs, decl);
7239             e.cast.exp = thisExp;
7240             e.byReference = true;
7241             e.expType = type;
7242             type.refCount++;
7243          }
7244          e.destType = destType;
7245          destType.refCount++;
7246       }
7247    }
7248 }
7249
7250 void ProcessExpressionType(Expression exp)
7251 {
7252    bool unresolved = false;
7253    Location oldyylloc = yylloc;
7254    bool notByReference = false;
7255 #ifdef _DEBUG
7256    char debugExpString[4096];
7257    debugExpString[0] = '\0';
7258    PrintExpression(exp, debugExpString);
7259 #endif
7260    if(!exp || exp.expType)
7261       return;
7262
7263    //eSystem_Logf("%s\n", expString);
7264
7265    // Testing this here
7266    yylloc = exp.loc;
7267    switch(exp.type)
7268    {
7269       case identifierExp:
7270       {
7271          Identifier id = exp.identifier;
7272          if(!id || !topContext) return;
7273
7274          // DOING THIS LATER NOW...
7275          if(id._class && id._class.name)
7276          {
7277             id.classSym = id._class.symbol; // FindClass(id._class.name);
7278             /* TODO: Name Space Fix ups
7279             if(!id.classSym)
7280                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7281             */
7282          }
7283
7284          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7285          {
7286             exp.expType = ProcessTypeString("Module", true);
7287             break;
7288          }
7289          else */if(strstr(id.string, "__ecereClass") == id.string)
7290          {
7291             exp.expType = ProcessTypeString("ecere::com::Class", true);
7292             break;
7293          }
7294          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7295          {
7296             // Added this here as well
7297             ReplaceClassMembers(exp, thisClass);
7298             if(exp.type != identifierExp)
7299             {
7300                ProcessExpressionType(exp);
7301                break;
7302             }
7303
7304             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7305                break;
7306          }
7307          else
7308          {
7309             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7310             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7311             if(!symbol/* && exp.destType*/)
7312             {
7313                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7314                   break;
7315                else
7316                {
7317                   if(thisClass)
7318                   {
7319                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7320                      if(exp.type != identifierExp)
7321                      {
7322                         ProcessExpressionType(exp);
7323                         break;
7324                      }
7325                   }
7326                   // Static methods called from inside the _class
7327                   else if(currentClass && !id._class)
7328                   {
7329                      if(ResolveIdWithClass(exp, currentClass, true))
7330                         break;
7331                   }
7332                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7333                }
7334             }
7335
7336             // If we manage to resolve this symbol
7337             if(symbol)
7338             {
7339                Type type = symbol.type;
7340                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7341
7342                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7343                {
7344                   Context context = SetupTemplatesContext(_class);
7345                   type = ReplaceThisClassType(_class);
7346                   FinishTemplatesContext(context);
7347                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7348                }
7349
7350                FreeSpecifier(id._class);
7351                id._class = null;
7352                delete id.string;
7353                id.string = CopyString(symbol.string);
7354
7355                id.classSym = null;
7356                exp.expType = type;
7357                if(type)
7358                   type.refCount++;
7359                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7360                   // Add missing cases here... enum Classes...
7361                   exp.isConstant = true;
7362
7363                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7364                if(symbol.isParam || !strcmp(id.string, "this"))
7365                {
7366                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7367                      exp.byReference = true;
7368
7369                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7370                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7371                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7372                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7373                   {
7374                      Identifier id = exp.identifier;
7375                      exp.type = bracketsExp;
7376                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7377                   }*/
7378                }
7379
7380                if(symbol.isIterator)
7381                {
7382                   if(symbol.isIterator == 3)
7383                   {
7384                      exp.type = bracketsExp;
7385                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7386                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7387                      exp.expType = null;
7388                      ProcessExpressionType(exp);
7389                   }
7390                   else if(symbol.isIterator != 4)
7391                   {
7392                      exp.type = memberExp;
7393                      exp.member.exp = MkExpIdentifier(exp.identifier);
7394                      exp.member.exp.expType = exp.expType;
7395                      /*if(symbol.isIterator == 6)
7396                         exp.member.member = MkIdentifier("key");
7397                      else*/
7398                         exp.member.member = MkIdentifier("data");
7399                      exp.expType = null;
7400                      ProcessExpressionType(exp);
7401                   }
7402                }
7403                break;
7404             }
7405             else
7406             {
7407                DefinedExpression definedExp = null;
7408                if(thisNameSpace && !(id._class && !id._class.name))
7409                {
7410                   char name[1024];
7411                   strcpy(name, thisNameSpace);
7412                   strcat(name, "::");
7413                   strcat(name, id.string);
7414                   definedExp = eSystem_FindDefine(privateModule, name);
7415                }
7416                if(!definedExp)
7417                   definedExp = eSystem_FindDefine(privateModule, id.string);
7418                if(definedExp)
7419                {
7420                   int c;
7421                   for(c = 0; c<definedExpStackPos; c++)
7422                      if(definedExpStack[c] == definedExp)
7423                         break;
7424                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7425                   {
7426                      Location backupYylloc = yylloc;
7427                      File backInput = fileInput;
7428                      definedExpStack[definedExpStackPos++] = definedExp;
7429
7430                      fileInput = TempFile { };
7431                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7432                      fileInput.Seek(0, start);
7433
7434                      echoOn = false;
7435                      parsedExpression = null;
7436                      resetScanner();
7437                      expression_yyparse();
7438                      delete fileInput;
7439                      if(backInput)
7440                         fileInput = backInput;
7441
7442                      yylloc = backupYylloc;
7443
7444                      if(parsedExpression)
7445                      {
7446                         FreeIdentifier(id);
7447                         exp.type = bracketsExp;
7448                         exp.list = MkListOne(parsedExpression);
7449                         parsedExpression.loc = yylloc;
7450                         ProcessExpressionType(exp);
7451                         definedExpStackPos--;
7452                         return;
7453                      }
7454                      definedExpStackPos--;
7455                   }
7456                   else
7457                   {
7458                      if(inCompiler)
7459                      {
7460                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7461                      }
7462                   }
7463                }
7464                else
7465                {
7466                   GlobalData data = null;
7467                   if(thisNameSpace && !(id._class && !id._class.name))
7468                   {
7469                      char name[1024];
7470                      strcpy(name, thisNameSpace);
7471                      strcat(name, "::");
7472                      strcat(name, id.string);
7473                      data = FindGlobalData(name);
7474                   }
7475                   if(!data)
7476                      data = FindGlobalData(id.string);
7477                   if(data)
7478                   {
7479                      DeclareGlobalData(data);
7480                      exp.expType = data.dataType;
7481                      if(data.dataType) data.dataType.refCount++;
7482
7483                      delete id.string;
7484                      id.string = CopyString(data.fullName);
7485                      FreeSpecifier(id._class);
7486                      id._class = null;
7487
7488                      break;
7489                   }
7490                   else
7491                   {
7492                      GlobalFunction function = null;
7493                      if(thisNameSpace && !(id._class && !id._class.name))
7494                      {
7495                         char name[1024];
7496                         strcpy(name, thisNameSpace);
7497                         strcat(name, "::");
7498                         strcat(name, id.string);
7499                         function = eSystem_FindFunction(privateModule, name);
7500                      }
7501                      if(!function)
7502                         function = eSystem_FindFunction(privateModule, id.string);
7503                      if(function)
7504                      {
7505                         char name[1024];
7506                         delete id.string;
7507                         id.string = CopyString(function.name);
7508                         name[0] = 0;
7509
7510                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7511                            strcpy(name, "__ecereFunction_");
7512                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7513                         if(DeclareFunction(function, name))
7514                         {
7515                            delete id.string;
7516                            id.string = CopyString(name);
7517                         }
7518                         exp.expType = function.dataType;
7519                         if(function.dataType) function.dataType.refCount++;
7520
7521                         FreeSpecifier(id._class);
7522                         id._class = null;
7523
7524                         break;
7525                      }
7526                   }
7527                }
7528             }
7529          }
7530          unresolved = true;
7531          break;
7532       }
7533       case instanceExp:
7534       {
7535          Class _class;
7536          // Symbol classSym;
7537
7538          if(!exp.instance._class)
7539          {
7540             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7541             {
7542                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7543             }
7544          }
7545
7546          //classSym = FindClass(exp.instance._class.fullName);
7547          //_class = classSym ? classSym.registered : null;
7548
7549          ProcessInstantiationType(exp.instance);
7550          exp.isConstant = exp.instance.isConstant;
7551
7552          /*
7553          if(_class.type == unitClass && _class.base.type != systemClass)
7554          {
7555             {
7556                Type destType = exp.destType;
7557
7558                exp.destType = MkClassType(_class.base.fullName);
7559                exp.expType = MkClassType(_class.fullName);
7560                CheckExpressionType(exp, exp.destType, true);
7561
7562                exp.destType = destType;
7563             }
7564             exp.expType = MkClassType(_class.fullName);
7565          }
7566          else*/
7567          if(exp.instance._class)
7568          {
7569             exp.expType = MkClassType(exp.instance._class.name);
7570             /*if(exp.expType._class && exp.expType._class.registered &&
7571                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7572                exp.expType.byReference = true;*/
7573          }
7574          break;
7575       }
7576       case constantExp:
7577       {
7578          if(!exp.expType)
7579          {
7580             char * constant = exp.constant;
7581             Type type
7582             {
7583                refCount = 1;
7584                constant = true;
7585             };
7586             exp.expType = type;
7587
7588             if(constant[0] == '\'')
7589             {
7590                if((int)((byte *)constant)[1] > 127)
7591                {
7592                   int nb;
7593                   unichar ch = UTF8GetChar(constant + 1, &nb);
7594                   if(nb < 2) ch = constant[1];
7595                   delete constant;
7596                   exp.constant = PrintUInt(ch);
7597                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7598                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7599                   type._class = FindClass("unichar");
7600
7601                   type.isSigned = false;
7602                }
7603                else
7604                {
7605                   type.kind = charType;
7606                   type.isSigned = true;
7607                }
7608             }
7609             else
7610             {
7611                char * dot = strchr(constant, '.');
7612                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7613                char * exponent;
7614                if(isHex)
7615                {
7616                   exponent = strchr(constant, 'p');
7617                   if(!exponent) exponent = strchr(constant, 'P');
7618                }
7619                else
7620                {
7621                   exponent = strchr(constant, 'e');
7622                   if(!exponent) exponent = strchr(constant, 'E');
7623                }
7624
7625                if(dot || exponent)
7626                {
7627                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7628                      type.kind = floatType;
7629                   else
7630                      type.kind = doubleType;
7631                   type.isSigned = true;
7632                }
7633                else
7634                {
7635                   bool isSigned = constant[0] == '-';
7636                   int64 i64 = strtoll(constant, null, 0);
7637                   uint64 ui64 = strtoull(constant, null, 0);
7638                   bool is64Bit = false;
7639                   if(isSigned)
7640                   {
7641                      if(i64 < MININT)
7642                         is64Bit = true;
7643                   }
7644                   else
7645                   {
7646                      if(ui64 > MAXINT)
7647                      {
7648                         if(ui64 > MAXDWORD)
7649                         {
7650                            is64Bit = true;
7651                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7652                               isSigned = true;
7653                         }
7654                      }
7655                      else if(constant[0] != '0' || !constant[1])
7656                         isSigned = true;
7657                   }
7658                   type.kind = is64Bit ? int64Type : intType;
7659                   type.isSigned = isSigned;
7660                }
7661             }
7662             exp.isConstant = true;
7663             if(exp.destType && exp.destType.kind == doubleType)
7664                type.kind = doubleType;
7665             else if(exp.destType && exp.destType.kind == floatType)
7666                type.kind = floatType;
7667             else if(exp.destType && exp.destType.kind == int64Type)
7668                type.kind = int64Type;
7669          }
7670          break;
7671       }
7672       case stringExp:
7673       {
7674          exp.isConstant = true;      // Why wasn't this constant?
7675          exp.expType = Type
7676          {
7677             refCount = 1;
7678             kind = pointerType;
7679             type = Type
7680             {
7681                refCount = 1;
7682                kind = charType;
7683                constant = true;
7684                isSigned = true;
7685             }
7686          };
7687          break;
7688       }
7689       case newExp:
7690       case new0Exp:
7691          ProcessExpressionType(exp._new.size);
7692          exp.expType = Type
7693          {
7694             refCount = 1;
7695             kind = pointerType;
7696             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7697          };
7698          DeclareType(exp.expType.type, false, false);
7699          break;
7700       case renewExp:
7701       case renew0Exp:
7702          ProcessExpressionType(exp._renew.size);
7703          ProcessExpressionType(exp._renew.exp);
7704          exp.expType = Type
7705          {
7706             refCount = 1;
7707             kind = pointerType;
7708             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7709          };
7710          DeclareType(exp.expType.type, false, false);
7711          break;
7712       case opExp:
7713       {
7714          bool assign = false, boolResult = false, boolOps = false;
7715          Type type1 = null, type2 = null;
7716          bool useDestType = false, useSideType = false;
7717          Location oldyylloc = yylloc;
7718          bool useSideUnit = false;
7719
7720          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7721          Type dummy
7722          {
7723             count = 1;
7724             refCount = 1;
7725          };
7726
7727          switch(exp.op.op)
7728          {
7729             // Assignment Operators
7730             case '=':
7731             case MUL_ASSIGN:
7732             case DIV_ASSIGN:
7733             case MOD_ASSIGN:
7734             case ADD_ASSIGN:
7735             case SUB_ASSIGN:
7736             case LEFT_ASSIGN:
7737             case RIGHT_ASSIGN:
7738             case AND_ASSIGN:
7739             case XOR_ASSIGN:
7740             case OR_ASSIGN:
7741                assign = true;
7742                break;
7743             // boolean Operators
7744             case '!':
7745                // Expect boolean operators
7746                //boolOps = true;
7747                //boolResult = true;
7748                break;
7749             case AND_OP:
7750             case OR_OP:
7751                // Expect boolean operands
7752                boolOps = true;
7753                boolResult = true;
7754                break;
7755             // Comparisons
7756             case EQ_OP:
7757             case '<':
7758             case '>':
7759             case LE_OP:
7760             case GE_OP:
7761             case NE_OP:
7762                // Gives boolean result
7763                boolResult = true;
7764                useSideType = true;
7765                break;
7766             case '+':
7767             case '-':
7768                useSideUnit = true;
7769
7770                // Just added these... testing
7771             case '|':
7772             case '&':
7773             case '^':
7774
7775             // DANGER: Verify units
7776             case '/':
7777             case '%':
7778             case '*':
7779
7780                if(exp.op.op != '*' || exp.op.exp1)
7781                {
7782                   useSideType = true;
7783                   useDestType = true;
7784                }
7785                break;
7786
7787             /*// Implement speed etc.
7788             case '*':
7789             case '/':
7790                break;
7791             */
7792          }
7793          if(exp.op.op == '&')
7794          {
7795             // Added this here earlier for Iterator address as key
7796             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
7797             {
7798                Identifier id = exp.op.exp2.identifier;
7799                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7800                if(symbol && symbol.isIterator == 2)
7801                {
7802                   exp.type = memberExp;
7803                   exp.member.exp = exp.op.exp2;
7804                   exp.member.member = MkIdentifier("key");
7805                   exp.expType = null;
7806                   exp.op.exp2.expType = symbol.type;
7807                   symbol.type.refCount++;
7808                   ProcessExpressionType(exp);
7809                   FreeType(dummy);
7810                   break;
7811                }
7812                // exp.op.exp2.usage.usageRef = true;
7813             }
7814          }
7815
7816          //dummy.kind = TypeDummy;
7817
7818          if(exp.op.exp1)
7819          {
7820             if(exp.destType && exp.destType.kind == classType &&
7821                exp.destType._class && exp.destType._class.registered && useDestType &&
7822
7823               ((exp.destType._class.registered.type == unitClass && useSideUnit) ||
7824                exp.destType._class.registered.type == enumClass ||
7825                exp.destType._class.registered.type == bitClass
7826                ))
7827
7828               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
7829             {
7830                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7831                exp.op.exp1.destType = exp.destType;
7832                if(exp.destType)
7833                   exp.destType.refCount++;
7834             }
7835             else if(!assign)
7836             {
7837                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7838                exp.op.exp1.destType = dummy;
7839                dummy.refCount++;
7840             }
7841
7842             // TESTING THIS HERE...
7843             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
7844             ProcessExpressionType(exp.op.exp1);
7845             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
7846
7847             if(exp.op.exp1.destType == dummy)
7848             {
7849                FreeType(dummy);
7850                exp.op.exp1.destType = null;
7851             }
7852             type1 = exp.op.exp1.expType;
7853          }
7854
7855          if(exp.op.exp2)
7856          {
7857             char expString[10240];
7858             expString[0] = '\0';
7859             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
7860             {
7861                if(exp.op.exp1)
7862                {
7863                   exp.op.exp2.destType = exp.op.exp1.expType;
7864                   if(exp.op.exp1.expType)
7865                      exp.op.exp1.expType.refCount++;
7866                }
7867                else
7868                {
7869                   exp.op.exp2.destType = exp.destType;
7870                   if(exp.destType)
7871                      exp.destType.refCount++;
7872                }
7873
7874                if(type1) type1.refCount++;
7875                exp.expType = type1;
7876             }
7877             else if(assign)
7878             {
7879                if(inCompiler)
7880                   PrintExpression(exp.op.exp2, expString);
7881
7882                if(type1 && type1.kind == pointerType)
7883                {
7884                   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 ||
7885                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
7886                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
7887                   else if(exp.op.op == '=')
7888                   {
7889                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7890                      exp.op.exp2.destType = type1;
7891                      if(type1)
7892                         type1.refCount++;
7893                   }
7894                }
7895                else
7896                {
7897                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
7898                   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/* ||
7899                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
7900                   else
7901                   {
7902                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7903                      exp.op.exp2.destType = type1;
7904                      if(type1)
7905                         type1.refCount++;
7906                   }
7907                }
7908                if(type1) type1.refCount++;
7909                exp.expType = type1;
7910             }
7911             else if(exp.destType && exp.destType.kind == classType &&
7912                exp.destType._class && exp.destType._class.registered &&
7913
7914                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) ||
7915                   (exp.destType._class.registered.type == enumClass && useDestType))
7916                   )
7917             {
7918                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7919                exp.op.exp2.destType = exp.destType;
7920                if(exp.destType)
7921                   exp.destType.refCount++;
7922             }
7923             else
7924             {
7925                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7926                exp.op.exp2.destType = dummy;
7927                dummy.refCount++;
7928             }
7929
7930             // TESTING THIS HERE... (DANGEROUS)
7931             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
7932                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
7933             {
7934                FreeType(exp.op.exp2.destType);
7935                exp.op.exp2.destType = type1;
7936                type1.refCount++;
7937             }
7938             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
7939             ProcessExpressionType(exp.op.exp2);
7940             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
7941
7942             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
7943             {
7944                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)
7945                {
7946                   if(exp.op.op != '=' && type1.type.kind == voidType)
7947                      Compiler_Error($"void *: unknown size\n");
7948                }
7949                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||
7950                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
7951                               (exp.op.exp2.expType._class.registered.type == normalClass ||
7952                               exp.op.exp2.expType._class.registered.type == structClass ||
7953                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
7954                {
7955                   if(exp.op.op == ADD_ASSIGN)
7956                      Compiler_Error($"cannot add two pointers\n");
7957                }
7958                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
7959                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
7960                {
7961                   if(exp.op.op == ADD_ASSIGN)
7962                      Compiler_Error($"cannot add two pointers\n");
7963                }
7964                else if(inCompiler)
7965                {
7966                   char type1String[1024];
7967                   char type2String[1024];
7968                   type1String[0] = '\0';
7969                   type2String[0] = '\0';
7970
7971                   PrintType(exp.op.exp2.expType, type1String, false, true);
7972                   PrintType(type1, type2String, false, true);
7973                   ChangeCh(expString, '\n', ' ');
7974                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
7975                }
7976             }
7977
7978             if(exp.op.exp2.destType == dummy)
7979             {
7980                FreeType(dummy);
7981                exp.op.exp2.destType = null;
7982             }
7983
7984             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
7985             {
7986                type2 = { };
7987                type2.refCount = 1;
7988                CopyTypeInto(type2, exp.op.exp2.expType);
7989                type2.isSigned = true;
7990             }
7991             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
7992             {
7993                type2 = { kind = intType };
7994                type2.refCount = 1;
7995                type2.isSigned = true;
7996             }
7997             else
7998             {
7999                type2 = exp.op.exp2.expType;
8000                if(type2) type2.refCount++;
8001             }
8002          }
8003
8004          dummy.kind = voidType;
8005
8006          if(exp.op.op == SIZEOF)
8007          {
8008             exp.expType = Type
8009             {
8010                refCount = 1;
8011                kind = intType;
8012             };
8013             exp.isConstant = true;
8014          }
8015          // Get type of dereferenced pointer
8016          else if(exp.op.op == '*' && !exp.op.exp1)
8017          {
8018             exp.expType = Dereference(type2);
8019             if(type2 && type2.kind == classType)
8020                notByReference = true;
8021          }
8022          else if(exp.op.op == '&' && !exp.op.exp1)
8023             exp.expType = Reference(type2);
8024          else if(!assign)
8025          {
8026             if(boolOps)
8027             {
8028                if(exp.op.exp1)
8029                {
8030                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8031                   exp.op.exp1.destType = MkClassType("bool");
8032                   exp.op.exp1.destType.truth = true;
8033                   if(!exp.op.exp1.expType)
8034                      ProcessExpressionType(exp.op.exp1);
8035                   else
8036                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8037                   FreeType(exp.op.exp1.expType);
8038                   exp.op.exp1.expType = MkClassType("bool");
8039                   exp.op.exp1.expType.truth = true;
8040                }
8041                if(exp.op.exp2)
8042                {
8043                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8044                   exp.op.exp2.destType = MkClassType("bool");
8045                   exp.op.exp2.destType.truth = true;
8046                   if(!exp.op.exp2.expType)
8047                      ProcessExpressionType(exp.op.exp2);
8048                   else
8049                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8050                   FreeType(exp.op.exp2.expType);
8051                   exp.op.exp2.expType = MkClassType("bool");
8052                   exp.op.exp2.expType.truth = true;
8053                }
8054             }
8055             else if(exp.op.exp1 && exp.op.exp2 &&
8056                ((useSideType /*&&
8057                      (useSideUnit ||
8058                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8059                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8060                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8061                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8062             {
8063                if(type1 && type2 &&
8064                   // If either both are class or both are not class
8065                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8066                {
8067                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8068                   exp.op.exp2.destType = type1;
8069                   type1.refCount++;
8070                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8071                   exp.op.exp1.destType = type2;
8072                   type2.refCount++;
8073                   // Warning here for adding Radians + Degrees with no destination type
8074                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8075                      type1._class.registered && type1._class.registered.type == unitClass &&
8076                      type2._class.registered && type2._class.registered.type == unitClass &&
8077                      type1._class.registered != type2._class.registered)
8078                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8079                         type1._class.string, type2._class.string, type1._class.string);
8080
8081                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8082                   {
8083                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8084                      if(argExp)
8085                      {
8086                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8087
8088                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8089                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8090                            exp.op.exp1)));
8091
8092                         ProcessExpressionType(exp.op.exp1);
8093
8094                         if(type2.kind != pointerType)
8095                         {
8096                            ProcessExpressionType(classExp);
8097
8098                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8099                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8100                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8101                                  // noHeadClass
8102                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8103                                     OR_OP,
8104                                  // normalClass
8105                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8106                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8107                                        MkPointer(null, null), null)))),
8108                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8109
8110                            if(!exp.op.exp2.expType)
8111                            {
8112                               if(type2)
8113                                  FreeType(type2);
8114                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8115                               type2.refCount++;
8116                            }
8117
8118                            ProcessExpressionType(exp.op.exp2);
8119                         }
8120                      }
8121                   }
8122
8123                   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)))
8124                   {
8125                      if(type1.kind != classType && type1.type.kind == voidType)
8126                         Compiler_Error($"void *: unknown size\n");
8127                      exp.expType = type1;
8128                      if(type1) type1.refCount++;
8129                   }
8130                   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)))
8131                   {
8132                      if(type2.kind != classType && type2.type.kind == voidType)
8133                         Compiler_Error($"void *: unknown size\n");
8134                      exp.expType = type2;
8135                      if(type2) type2.refCount++;
8136                   }
8137                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8138                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8139                   {
8140                      Compiler_Warning($"different levels of indirection\n");
8141                   }
8142                   else
8143                   {
8144                      bool success = false;
8145                      if(type1.kind == pointerType && type2.kind == pointerType)
8146                      {
8147                         if(exp.op.op == '+')
8148                            Compiler_Error($"cannot add two pointers\n");
8149                         else if(exp.op.op == '-')
8150                         {
8151                            // Pointer Subtraction gives integer
8152                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8153                            {
8154                               exp.expType = Type
8155                               {
8156                                  kind = intType;
8157                                  refCount = 1;
8158                               };
8159                               success = true;
8160
8161                               if(type1.type.kind == templateType)
8162                               {
8163                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8164                                  if(argExp)
8165                                  {
8166                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8167
8168                                     ProcessExpressionType(classExp);
8169
8170                                     exp.type = bracketsExp;
8171                                     exp.list = MkListOne(MkExpOp(
8172                                        MkExpBrackets(MkListOne(MkExpOp(
8173                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8174                                              , exp.op.op,
8175                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8176
8177                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8178
8179                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8180                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8181                                                 // noHeadClass
8182                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8183                                                    OR_OP,
8184                                                 // normalClass
8185                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8186                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8187                                                       MkPointer(null, null), null)))),
8188                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8189
8190
8191                                              ));
8192
8193                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8194                                     FreeType(dummy);
8195                                     return;
8196                                  }
8197                               }
8198                            }
8199                         }
8200                      }
8201
8202                      if(!success && exp.op.exp1.type == constantExp)
8203                      {
8204                         // If first expression is constant, try to match that first
8205                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8206                         {
8207                            if(exp.expType) FreeType(exp.expType);
8208                            exp.expType = exp.op.exp1.destType;
8209                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8210                            success = true;
8211                         }
8212                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8213                         {
8214                            if(exp.expType) FreeType(exp.expType);
8215                            exp.expType = exp.op.exp2.destType;
8216                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8217                            success = true;
8218                         }
8219                      }
8220                      else if(!success)
8221                      {
8222                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8223                         {
8224                            if(exp.expType) FreeType(exp.expType);
8225                            exp.expType = exp.op.exp2.destType;
8226                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8227                            success = true;
8228                         }
8229                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8230                         {
8231                            if(exp.expType) FreeType(exp.expType);
8232                            exp.expType = exp.op.exp1.destType;
8233                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8234                            success = true;
8235                         }
8236                      }
8237                      if(!success)
8238                      {
8239                         char expString1[10240];
8240                         char expString2[10240];
8241                         char type1[1024];
8242                         char type2[1024];
8243                         expString1[0] = '\0';
8244                         expString2[0] = '\0';
8245                         type1[0] = '\0';
8246                         type2[0] = '\0';
8247                         if(inCompiler)
8248                         {
8249                            PrintExpression(exp.op.exp1, expString1);
8250                            ChangeCh(expString1, '\n', ' ');
8251                            PrintExpression(exp.op.exp2, expString2);
8252                            ChangeCh(expString2, '\n', ' ');
8253                            PrintType(exp.op.exp1.expType, type1, false, true);
8254                            PrintType(exp.op.exp2.expType, type2, false, true);
8255                         }
8256
8257                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8258                      }
8259                   }
8260                }
8261                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8262                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8263                {
8264                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8265                   // Convert e.g. / 4 into / 4.0
8266                   exp.op.exp1.destType = type2._class.registered.dataType;
8267                   if(type2._class.registered.dataType)
8268                      type2._class.registered.dataType.refCount++;
8269                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8270                   exp.expType = type2;
8271                   if(type2) type2.refCount++;
8272                }
8273                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8274                {
8275                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8276                   // Convert e.g. / 4 into / 4.0
8277                   exp.op.exp2.destType = type1._class.registered.dataType;
8278                   if(type1._class.registered.dataType)
8279                      type1._class.registered.dataType.refCount++;
8280                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8281                   exp.expType = type1;
8282                   if(type1) type1.refCount++;
8283                }
8284                else if(type1)
8285                {
8286                   bool valid = false;
8287
8288                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8289                   {
8290                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8291
8292                      if(!type1._class.registered.dataType)
8293                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8294                      exp.op.exp2.destType = type1._class.registered.dataType;
8295                      exp.op.exp2.destType.refCount++;
8296
8297                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8298                      if(type2)
8299                         FreeType(type2);
8300                      type2 = exp.op.exp2.destType;
8301                      if(type2) type2.refCount++;
8302
8303                      exp.expType = type2;
8304                      type2.refCount++;
8305                   }
8306
8307                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8308                   {
8309                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8310
8311                      if(!type2._class.registered.dataType)
8312                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8313                      exp.op.exp1.destType = type2._class.registered.dataType;
8314                      exp.op.exp1.destType.refCount++;
8315
8316                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8317                      type1 = exp.op.exp1.destType;
8318                      exp.expType = type1;
8319                      type1.refCount++;
8320                   }
8321
8322                   // TESTING THIS NEW CODE
8323                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8324                   {
8325                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8326                      {
8327                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8328                         {
8329                            if(exp.expType) FreeType(exp.expType);
8330                            exp.expType = exp.op.exp1.expType;
8331                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8332                            valid = true;
8333                         }
8334                      }
8335
8336                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8337                      {
8338                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8339                         {
8340                            if(exp.expType) FreeType(exp.expType);
8341                            exp.expType = exp.op.exp2.expType;
8342                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8343                            valid = true;
8344                         }
8345                      }
8346                   }
8347
8348                   if(!valid)
8349                   {
8350                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8351                      exp.op.exp2.destType = type1;
8352                      type1.refCount++;
8353
8354                      /*
8355                      // Maybe this was meant to be an enum...
8356                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8357                      {
8358                         Type oldType = exp.op.exp2.expType;
8359                         exp.op.exp2.expType = null;
8360                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8361                            FreeType(oldType);
8362                         else
8363                            exp.op.exp2.expType = oldType;
8364                      }
8365                      */
8366
8367                      /*
8368                      // TESTING THIS HERE... LATEST ADDITION
8369                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8370                      {
8371                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8372                         exp.op.exp2.destType = type2._class.registered.dataType;
8373                         if(type2._class.registered.dataType)
8374                            type2._class.registered.dataType.refCount++;
8375                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8376
8377                         //exp.expType = type2._class.registered.dataType; //type2;
8378                         //if(type2) type2.refCount++;
8379                      }
8380
8381                      // TESTING THIS HERE... LATEST ADDITION
8382                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8383                      {
8384                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8385                         exp.op.exp1.destType = type1._class.registered.dataType;
8386                         if(type1._class.registered.dataType)
8387                            type1._class.registered.dataType.refCount++;
8388                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8389                         exp.expType = type1._class.registered.dataType; //type1;
8390                         if(type1) type1.refCount++;
8391                      }
8392                      */
8393
8394                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8395                      {
8396                         if(exp.expType) FreeType(exp.expType);
8397                         exp.expType = exp.op.exp2.destType;
8398                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8399                      }
8400                      else if(type1 && type2)
8401                      {
8402                         char expString1[10240];
8403                         char expString2[10240];
8404                         char type1String[1024];
8405                         char type2String[1024];
8406                         expString1[0] = '\0';
8407                         expString2[0] = '\0';
8408                         type1String[0] = '\0';
8409                         type2String[0] = '\0';
8410                         if(inCompiler)
8411                         {
8412                            PrintExpression(exp.op.exp1, expString1);
8413                            ChangeCh(expString1, '\n', ' ');
8414                            PrintExpression(exp.op.exp2, expString2);
8415                            ChangeCh(expString2, '\n', ' ');
8416                            PrintType(exp.op.exp1.expType, type1String, false, true);
8417                            PrintType(exp.op.exp2.expType, type2String, false, true);
8418                         }
8419
8420                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8421
8422                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8423                         {
8424                            exp.expType = exp.op.exp1.expType;
8425                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8426                         }
8427                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8428                         {
8429                            exp.expType = exp.op.exp2.expType;
8430                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8431                         }
8432                      }
8433                   }
8434                }
8435                else if(type2)
8436                {
8437                   // Maybe this was meant to be an enum...
8438                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8439                   {
8440                      Type oldType = exp.op.exp1.expType;
8441                      exp.op.exp1.expType = null;
8442                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8443                         FreeType(oldType);
8444                      else
8445                         exp.op.exp1.expType = oldType;
8446                   }
8447
8448                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8449                   exp.op.exp1.destType = type2;
8450                   type2.refCount++;
8451                   /*
8452                   // TESTING THIS HERE... LATEST ADDITION
8453                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8454                   {
8455                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8456                      exp.op.exp1.destType = type1._class.registered.dataType;
8457                      if(type1._class.registered.dataType)
8458                         type1._class.registered.dataType.refCount++;
8459                   }
8460
8461                   // TESTING THIS HERE... LATEST ADDITION
8462                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8463                   {
8464                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8465                      exp.op.exp2.destType = type2._class.registered.dataType;
8466                      if(type2._class.registered.dataType)
8467                         type2._class.registered.dataType.refCount++;
8468                   }
8469                   */
8470
8471                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8472                   {
8473                      if(exp.expType) FreeType(exp.expType);
8474                      exp.expType = exp.op.exp1.destType;
8475                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8476                   }
8477                }
8478             }
8479             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8480             {
8481                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8482                {
8483                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8484                   // Convert e.g. / 4 into / 4.0
8485                   exp.op.exp1.destType = type2._class.registered.dataType;
8486                   if(type2._class.registered.dataType)
8487                      type2._class.registered.dataType.refCount++;
8488                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8489                }
8490                if(exp.op.op == '!')
8491                {
8492                   exp.expType = MkClassType("bool");
8493                   exp.expType.truth = true;
8494                }
8495                else
8496                {
8497                   exp.expType = type2;
8498                   if(type2) type2.refCount++;
8499                }
8500             }
8501             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8502             {
8503                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8504                {
8505                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8506                   // Convert e.g. / 4 into / 4.0
8507                   exp.op.exp2.destType = type1._class.registered.dataType;
8508                   if(type1._class.registered.dataType)
8509                      type1._class.registered.dataType.refCount++;
8510                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8511                }
8512                exp.expType = type1;
8513                if(type1) type1.refCount++;
8514             }
8515          }
8516
8517          yylloc = exp.loc;
8518          if(exp.op.exp1 && !exp.op.exp1.expType)
8519          {
8520             char expString[10000];
8521             expString[0] = '\0';
8522             if(inCompiler)
8523             {
8524                PrintExpression(exp.op.exp1, expString);
8525                ChangeCh(expString, '\n', ' ');
8526             }
8527             if(expString[0])
8528                Compiler_Error($"couldn't determine type of %s\n", expString);
8529          }
8530          if(exp.op.exp2 && !exp.op.exp2.expType)
8531          {
8532             char expString[10240];
8533             expString[0] = '\0';
8534             if(inCompiler)
8535             {
8536                PrintExpression(exp.op.exp2, expString);
8537                ChangeCh(expString, '\n', ' ');
8538             }
8539             if(expString[0])
8540                Compiler_Error($"couldn't determine type of %s\n", expString);
8541          }
8542
8543          if(boolResult)
8544          {
8545             FreeType(exp.expType);
8546             exp.expType = MkClassType("bool");
8547             exp.expType.truth = true;
8548          }
8549
8550          if(exp.op.op != SIZEOF)
8551             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8552                (!exp.op.exp2 || exp.op.exp2.isConstant);
8553
8554          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8555          {
8556             DeclareType(exp.op.exp2.expType, false, false);
8557          }
8558
8559          yylloc = oldyylloc;
8560
8561          FreeType(dummy);
8562          if(type2)
8563             FreeType(type2);
8564          break;
8565       }
8566       case bracketsExp:
8567       case extensionExpressionExp:
8568       {
8569          Expression e;
8570          exp.isConstant = true;
8571          for(e = exp.list->first; e; e = e.next)
8572          {
8573             bool inced = false;
8574             if(!e.next)
8575             {
8576                FreeType(e.destType);
8577                e.destType = exp.destType;
8578                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8579             }
8580             ProcessExpressionType(e);
8581             if(inced)
8582                exp.destType.count--;
8583             if(!exp.expType && !e.next)
8584             {
8585                exp.expType = e.expType;
8586                if(e.expType) e.expType.refCount++;
8587             }
8588             if(!e.isConstant)
8589                exp.isConstant = false;
8590          }
8591
8592          // In case a cast became a member...
8593          e = exp.list->first;
8594          if(!e.next && e.type == memberExp)
8595          {
8596             // Preserve prev, next
8597             Expression next = exp.next, prev = exp.prev;
8598
8599
8600             FreeType(exp.expType);
8601             FreeType(exp.destType);
8602             delete exp.list;
8603
8604             *exp = *e;
8605
8606             exp.prev = prev;
8607             exp.next = next;
8608
8609             delete e;
8610
8611             ProcessExpressionType(exp);
8612          }
8613          break;
8614       }
8615       case indexExp:
8616       {
8617          Expression e;
8618          exp.isConstant = true;
8619
8620          ProcessExpressionType(exp.index.exp);
8621          if(!exp.index.exp.isConstant)
8622             exp.isConstant = false;
8623
8624          if(exp.index.exp.expType)
8625          {
8626             Type source = exp.index.exp.expType;
8627             if(source.kind == classType && source._class && source._class.registered)
8628             {
8629                Class _class = source._class.registered;
8630                Class c = _class.templateClass ? _class.templateClass : _class;
8631                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
8632                {
8633                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8634
8635                   if(exp.index.index && exp.index.index->last)
8636                   {
8637                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8638                   }
8639                }
8640             }
8641          }
8642
8643          for(e = exp.index.index->first; e; e = e.next)
8644          {
8645             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8646             {
8647                if(e.destType) FreeType(e.destType);
8648                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8649             }
8650             ProcessExpressionType(e);
8651             if(!e.next)
8652             {
8653                // Check if this type is int
8654             }
8655             if(!e.isConstant)
8656                exp.isConstant = false;
8657          }
8658
8659          if(!exp.expType)
8660             exp.expType = Dereference(exp.index.exp.expType);
8661          if(exp.expType)
8662             DeclareType(exp.expType, false, false);
8663          break;
8664       }
8665       case callExp:
8666       {
8667          Expression e;
8668          Type functionType;
8669          Type methodType = null;
8670          char name[1024];
8671          name[0] = '\0';
8672
8673          if(inCompiler)
8674          {
8675             PrintExpression(exp.call.exp,  name);
8676             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8677             {
8678                //exp.call.exp.expType = null;
8679                PrintExpression(exp.call.exp,  name);
8680             }
8681          }
8682          if(exp.call.exp.type == identifierExp)
8683          {
8684             Expression idExp = exp.call.exp;
8685             Identifier id = idExp.identifier;
8686             if(!strcmp(id.string, "__builtin_frame_address"))
8687             {
8688                exp.expType = ProcessTypeString("void *", true);
8689                if(exp.call.arguments && exp.call.arguments->first)
8690                   ProcessExpressionType(exp.call.arguments->first);
8691                break;
8692             }
8693             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8694             {
8695                exp.expType = ProcessTypeString("int", true);
8696                if(exp.call.arguments && exp.call.arguments->first)
8697                   ProcessExpressionType(exp.call.arguments->first);
8698                break;
8699             }
8700             else if(!strcmp(id.string, "Max") ||
8701                !strcmp(id.string, "Min") ||
8702                !strcmp(id.string, "Sgn") ||
8703                !strcmp(id.string, "Abs"))
8704             {
8705                Expression a = null;
8706                Expression b = null;
8707                Expression tempExp1 = null, tempExp2 = null;
8708                if((!strcmp(id.string, "Max") ||
8709                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8710                {
8711                   a = exp.call.arguments->first;
8712                   b = exp.call.arguments->last;
8713                   tempExp1 = a;
8714                   tempExp2 = b;
8715                }
8716                else if(exp.call.arguments->count == 1)
8717                {
8718                   a = exp.call.arguments->first;
8719                   tempExp1 = a;
8720                }
8721
8722                if(a)
8723                {
8724                   exp.call.arguments->Clear();
8725                   idExp.identifier = null;
8726
8727                   FreeExpContents(exp);
8728
8729                   ProcessExpressionType(a);
8730                   if(b)
8731                      ProcessExpressionType(b);
8732
8733                   exp.type = bracketsExp;
8734                   exp.list = MkList();
8735
8736                   if(a.expType && (!b || b.expType))
8737                   {
8738                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8739                      {
8740                         // Use the simpleStruct name/ids for now...
8741                         if(inCompiler)
8742                         {
8743                            OldList * specs = MkList();
8744                            OldList * decls = MkList();
8745                            Declaration decl;
8746                            char temp1[1024], temp2[1024];
8747
8748                            GetTypeSpecs(a.expType, specs);
8749
8750                            if(a && !a.isConstant && a.type != identifierExp)
8751                            {
8752                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8753                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8754                               tempExp1 = QMkExpId(temp1);
8755                               tempExp1.expType = a.expType;
8756                               if(a.expType)
8757                                  a.expType.refCount++;
8758                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
8759                            }
8760                            if(b && !b.isConstant && b.type != identifierExp)
8761                            {
8762                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
8763                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
8764                               tempExp2 = QMkExpId(temp2);
8765                               tempExp2.expType = b.expType;
8766                               if(b.expType)
8767                                  b.expType.refCount++;
8768                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
8769                            }
8770
8771                            decl = MkDeclaration(specs, decls);
8772                            if(!curCompound.compound.declarations)
8773                               curCompound.compound.declarations = MkList();
8774                            curCompound.compound.declarations->Insert(null, decl);
8775                         }
8776                      }
8777                   }
8778
8779                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
8780                   {
8781                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
8782                      ListAdd(exp.list,
8783                         MkExpCondition(MkExpBrackets(MkListOne(
8784                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
8785                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
8786                      exp.expType = a.expType;
8787                      if(a.expType)
8788                         a.expType.refCount++;
8789                   }
8790                   else if(!strcmp(id.string, "Abs"))
8791                   {
8792                      ListAdd(exp.list,
8793                         MkExpCondition(MkExpBrackets(MkListOne(
8794                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8795                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
8796                      exp.expType = a.expType;
8797                      if(a.expType)
8798                         a.expType.refCount++;
8799                   }
8800                   else if(!strcmp(id.string, "Sgn"))
8801                   {
8802                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
8803                      ListAdd(exp.list,
8804                         MkExpCondition(MkExpBrackets(MkListOne(
8805                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
8806                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
8807                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8808                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
8809                      exp.expType = ProcessTypeString("int", false);
8810                   }
8811
8812                   FreeExpression(tempExp1);
8813                   if(tempExp2) FreeExpression(tempExp2);
8814
8815                   FreeIdentifier(id);
8816                   break;
8817                }
8818             }
8819          }
8820
8821          {
8822             Type dummy
8823             {
8824                count = 1;
8825                refCount = 1;
8826             };
8827             if(!exp.call.exp.destType)
8828             {
8829                exp.call.exp.destType = dummy;
8830                dummy.refCount++;
8831             }
8832             ProcessExpressionType(exp.call.exp);
8833             if(exp.call.exp.destType == dummy)
8834             {
8835                FreeType(dummy);
8836                exp.call.exp.destType = null;
8837             }
8838             FreeType(dummy);
8839          }
8840
8841          // Check argument types against parameter types
8842          functionType = exp.call.exp.expType;
8843
8844          if(functionType && functionType.kind == TypeKind::methodType)
8845          {
8846             methodType = functionType;
8847             functionType = methodType.method.dataType;
8848
8849             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
8850             // TOCHECK: Instead of doing this here could this be done per param?
8851             if(exp.call.exp.expType.usedClass)
8852             {
8853                char typeString[1024];
8854                typeString[0] = '\0';
8855                {
8856                   Symbol back = functionType.thisClass;
8857                   // Do not output class specifier here (thisclass was added to this)
8858                   functionType.thisClass = null;
8859                   PrintType(functionType, typeString, true, true);
8860                   functionType.thisClass = back;
8861                }
8862                if(strstr(typeString, "thisclass"))
8863                {
8864                   OldList * specs = MkList();
8865                   Declarator decl;
8866                   {
8867                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
8868
8869                      decl = SpecDeclFromString(typeString, specs, null);
8870
8871                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
8872                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
8873                         exp.call.exp.expType.usedClass))
8874                         thisClassParams = false;
8875
8876                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
8877                      {
8878                         Class backupThisClass = thisClass;
8879                         thisClass = exp.call.exp.expType.usedClass;
8880                         ProcessDeclarator(decl);
8881                         thisClass = backupThisClass;
8882                      }
8883
8884                      thisClassParams = true;
8885
8886                      functionType = ProcessType(specs, decl);
8887                      functionType.refCount = 0;
8888                      FinishTemplatesContext(context);
8889                   }
8890
8891                   FreeList(specs, FreeSpecifier);
8892                   FreeDeclarator(decl);
8893                 }
8894             }
8895          }
8896          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
8897          {
8898             Type type = functionType.type;
8899             if(!functionType.refCount)
8900             {
8901                functionType.type = null;
8902                FreeType(functionType);
8903             }
8904             //methodType = functionType;
8905             functionType = type;
8906          }
8907          if(functionType && functionType.kind != TypeKind::functionType)
8908          {
8909             Compiler_Error($"called object %s is not a function\n", name);
8910          }
8911          else if(functionType)
8912          {
8913             bool emptyParams = false, noParams = false;
8914             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
8915             Type type = functionType.params.first;
8916             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
8917             int extra = 0;
8918             Location oldyylloc = yylloc;
8919
8920             if(!type) emptyParams = true;
8921
8922             // WORKING ON THIS:
8923             if(functionType.extraParam && e && functionType.thisClass)
8924             {
8925                e.destType = MkClassType(functionType.thisClass.string);
8926                e = e.next;
8927             }
8928
8929             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
8930             // Fixed #141 by adding '&& !functionType.extraParam'
8931             if(!functionType.staticMethod && !functionType.extraParam)
8932             {
8933                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
8934                   memberExp.member.exp.expType._class)
8935                {
8936                   type = MkClassType(memberExp.member.exp.expType._class.string);
8937                   if(e)
8938                   {
8939                      e.destType = type;
8940                      e = e.next;
8941                      type = functionType.params.first;
8942                   }
8943                   else
8944                      type.refCount = 0;
8945                }
8946                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
8947                {
8948                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
8949                   type.byReference = functionType.byReference;
8950                   type.typedByReference = functionType.typedByReference;
8951                   if(e)
8952                   {
8953                      // Allow manually passing a class for typed object
8954                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
8955                         e = e.next;
8956                      e.destType = type;
8957                      e = e.next;
8958                      type = functionType.params.first;
8959                   }
8960                   else
8961                      type.refCount = 0;
8962                   //extra = 1;
8963                }
8964             }
8965
8966             if(type && type.kind == voidType)
8967             {
8968                noParams = true;
8969                if(!type.refCount) FreeType(type);
8970                type = null;
8971             }
8972
8973             for( ; e; e = e.next)
8974             {
8975                if(!type && !emptyParams)
8976                {
8977                   yylloc = e.loc;
8978                   if(methodType && methodType.methodClass)
8979                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
8980                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
8981                         noParams ? 0 : functionType.params.count);
8982                   else
8983                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
8984                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
8985                         noParams ? 0 : functionType.params.count);
8986                   break;
8987                }
8988
8989                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
8990                {
8991                   Type templatedType = null;
8992                   Class _class = methodType.usedClass;
8993                   ClassTemplateParameter curParam = null;
8994                   int id = 0;
8995                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
8996                   {
8997                      Class sClass;
8998                      for(sClass = _class; sClass; sClass = sClass.base)
8999                      {
9000                         if(sClass.templateClass) sClass = sClass.templateClass;
9001                         id = 0;
9002                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9003                         {
9004                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9005                            {
9006                               Class nextClass;
9007                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9008                               {
9009                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9010                                  id += nextClass.templateParams.count;
9011                               }
9012                               break;
9013                            }
9014                            id++;
9015                         }
9016                         if(curParam) break;
9017                      }
9018                   }
9019                   if(curParam && _class.templateArgs[id].dataTypeString)
9020                   {
9021                      ClassTemplateArgument arg = _class.templateArgs[id];
9022                      {
9023                         Context context = SetupTemplatesContext(_class);
9024
9025                         /*if(!arg.dataType)
9026                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9027                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9028                         FinishTemplatesContext(context);
9029                      }
9030                      e.destType = templatedType;
9031                      if(templatedType)
9032                      {
9033                         templatedType.passAsTemplate = true;
9034                         // templatedType.refCount++;
9035                      }
9036                   }
9037                   else
9038                   {
9039                      e.destType = type;
9040                      if(type) type.refCount++;
9041                   }
9042                }
9043                else
9044                {
9045                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9046                   {
9047                      e.destType = type.prev;
9048                      e.destType.refCount++;
9049                   }
9050                   else
9051                   {
9052                      e.destType = type;
9053                      if(type) type.refCount++;
9054                   }
9055                }
9056                // Don't reach the end for the ellipsis
9057                if(type && type.kind != ellipsisType)
9058                {
9059                   Type next = type.next;
9060                   if(!type.refCount) FreeType(type);
9061                   type = next;
9062                }
9063             }
9064
9065             if(type && type.kind != ellipsisType)
9066             {
9067                if(methodType && methodType.methodClass)
9068                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9069                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9070                      functionType.params.count + extra);
9071                else
9072                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9073                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9074                      functionType.params.count + extra);
9075             }
9076             yylloc = oldyylloc;
9077             if(type && !type.refCount) FreeType(type);
9078          }
9079          else
9080          {
9081             functionType = Type
9082             {
9083                refCount = 0;
9084                kind = TypeKind::functionType;
9085             };
9086
9087             if(exp.call.exp.type == identifierExp)
9088             {
9089                char * string = exp.call.exp.identifier.string;
9090                if(inCompiler)
9091                {
9092                   Symbol symbol;
9093                   Location oldyylloc = yylloc;
9094
9095                   yylloc = exp.call.exp.identifier.loc;
9096                   if(strstr(string, "__builtin_") == string)
9097                   {
9098                      if(exp.destType)
9099                      {
9100                         functionType.returnType = exp.destType;
9101                         exp.destType.refCount++;
9102                      }
9103                   }
9104                   else
9105                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9106                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9107                   globalContext.symbols.Add((BTNode)symbol);
9108                   if(strstr(symbol.string, "::"))
9109                      globalContext.hasNameSpace = true;
9110
9111                   yylloc = oldyylloc;
9112                }
9113             }
9114             else if(exp.call.exp.type == memberExp)
9115             {
9116                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9117                   exp.call.exp.member.member.string);*/
9118             }
9119             else
9120                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9121
9122             if(!functionType.returnType)
9123             {
9124                functionType.returnType = Type
9125                {
9126                   refCount = 1;
9127                   kind = intType;
9128                };
9129             }
9130          }
9131          if(functionType && functionType.kind == TypeKind::functionType)
9132          {
9133             exp.expType = functionType.returnType;
9134
9135             if(functionType.returnType)
9136                functionType.returnType.refCount++;
9137
9138             if(!functionType.refCount)
9139                FreeType(functionType);
9140          }
9141
9142          if(exp.call.arguments)
9143          {
9144             for(e = exp.call.arguments->first; e; e = e.next)
9145             {
9146                Type destType = e.destType;
9147                ProcessExpressionType(e);
9148             }
9149          }
9150          break;
9151       }
9152       case memberExp:
9153       {
9154          Type type;
9155          Location oldyylloc = yylloc;
9156          bool thisPtr;
9157          Expression checkExp = exp.member.exp;
9158          while(checkExp)
9159          {
9160             if(checkExp.type == castExp)
9161                checkExp = checkExp.cast.exp;
9162             else if(checkExp.type == bracketsExp)
9163                checkExp = checkExp.list ? checkExp.list->first : null;
9164             else
9165                break;
9166          }
9167
9168          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9169          exp.thisPtr = thisPtr;
9170
9171          // DOING THIS LATER NOW...
9172          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9173          {
9174             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9175             /* TODO: Name Space Fix ups
9176             if(!exp.member.member.classSym)
9177                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9178             */
9179          }
9180
9181          ProcessExpressionType(exp.member.exp);
9182          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9183             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9184          {
9185             exp.isConstant = false;
9186          }
9187          else
9188             exp.isConstant = exp.member.exp.isConstant;
9189          type = exp.member.exp.expType;
9190
9191          yylloc = exp.loc;
9192
9193          if(type && (type.kind == templateType))
9194          {
9195             Class _class = thisClass ? thisClass : currentClass;
9196             ClassTemplateParameter param = null;
9197             if(_class)
9198             {
9199                for(param = _class.templateParams.first; param; param = param.next)
9200                {
9201                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9202                      break;
9203                }
9204             }
9205             if(param && param.defaultArg.member)
9206             {
9207                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9208                if(argExp)
9209                {
9210                   Expression expMember = exp.member.exp;
9211                   Declarator decl;
9212                   OldList * specs = MkList();
9213                   char thisClassTypeString[1024];
9214
9215                   FreeIdentifier(exp.member.member);
9216
9217                   ProcessExpressionType(argExp);
9218
9219                   {
9220                      char * colon = strstr(param.defaultArg.memberString, "::");
9221                      if(colon)
9222                      {
9223                         char className[1024];
9224                         Class sClass;
9225
9226                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9227                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9228                      }
9229                      else
9230                         strcpy(thisClassTypeString, _class.fullName);
9231                   }
9232
9233                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9234
9235                   exp.expType = ProcessType(specs, decl);
9236                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9237                   {
9238                      Class expClass = exp.expType._class.registered;
9239                      Class cClass = null;
9240                      int c;
9241                      int paramCount = 0;
9242                      int lastParam = -1;
9243
9244                      char templateString[1024];
9245                      ClassTemplateParameter param;
9246                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9247                      for(cClass = expClass; cClass; cClass = cClass.base)
9248                      {
9249                         int p = 0;
9250                         for(param = cClass.templateParams.first; param; param = param.next)
9251                         {
9252                            int id = p;
9253                            Class sClass;
9254                            ClassTemplateArgument arg;
9255                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9256                            arg = expClass.templateArgs[id];
9257
9258                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9259                            {
9260                               ClassTemplateParameter cParam;
9261                               //int p = numParams - sClass.templateParams.count;
9262                               int p = 0;
9263                               Class nextClass;
9264                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9265
9266                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9267                               {
9268                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9269                                  {
9270                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9271                                     {
9272                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9273                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9274                                        break;
9275                                     }
9276                                  }
9277                               }
9278                            }
9279
9280                            {
9281                               char argument[256];
9282                               argument[0] = '\0';
9283                               /*if(arg.name)
9284                               {
9285                                  strcat(argument, arg.name.string);
9286                                  strcat(argument, " = ");
9287                               }*/
9288                               switch(param.type)
9289                               {
9290                                  case expression:
9291                                  {
9292                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9293                                     char expString[1024];
9294                                     OldList * specs = MkList();
9295                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9296                                     Expression exp;
9297                                     char * string = PrintHexUInt64(arg.expression.ui64);
9298                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9299                                     delete string;
9300
9301                                     ProcessExpressionType(exp);
9302                                     ComputeExpression(exp);
9303                                     expString[0] = '\0';
9304                                     PrintExpression(exp, expString);
9305                                     strcat(argument, expString);
9306                                     // delete exp;
9307                                     FreeExpression(exp);
9308                                     break;
9309                                  }
9310                                  case identifier:
9311                                  {
9312                                     strcat(argument, arg.member.name);
9313                                     break;
9314                                  }
9315                                  case TemplateParameterType::type:
9316                                  {
9317                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9318                                     {
9319                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9320                                           strcat(argument, thisClassTypeString);
9321                                        else
9322                                           strcat(argument, arg.dataTypeString);
9323                                     }
9324                                     break;
9325                                  }
9326                               }
9327                               if(argument[0])
9328                               {
9329                                  if(paramCount) strcat(templateString, ", ");
9330                                  if(lastParam != p - 1)
9331                                  {
9332                                     strcat(templateString, param.name);
9333                                     strcat(templateString, " = ");
9334                                  }
9335                                  strcat(templateString, argument);
9336                                  paramCount++;
9337                                  lastParam = p;
9338                               }
9339                               p++;
9340                            }
9341                         }
9342                      }
9343                      {
9344                         int len = strlen(templateString);
9345                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9346                         templateString[len++] = '>';
9347                         templateString[len++] = '\0';
9348                      }
9349                      {
9350                         Context context = SetupTemplatesContext(_class);
9351                         FreeType(exp.expType);
9352                         exp.expType = ProcessTypeString(templateString, false);
9353                         FinishTemplatesContext(context);
9354                      }
9355                   }
9356
9357                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9358                   exp.type = bracketsExp;
9359                   exp.list = MkListOne(MkExpOp(null, '*',
9360                   /*opExp;
9361                   exp.op.op = '*';
9362                   exp.op.exp1 = null;
9363                   exp.op.exp2 = */
9364                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9365                      MkExpBrackets(MkListOne(
9366                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9367                            '+',
9368                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9369                            '+',
9370                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9371
9372                            ));
9373                }
9374             }
9375             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9376                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9377             {
9378                type = ProcessTemplateParameterType(type.templateParameter);
9379             }
9380          }
9381          // TODO: *** This seems to be where we should add method support for all basic types ***
9382          if(type && (type.kind == templateType));
9383          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9384                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9385                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9386                           (type.kind == pointerType && type.type.kind == charType)))
9387          {
9388             Identifier id = exp.member.member;
9389             TypeKind typeKind = type.kind;
9390             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9391             if(typeKind == subClassType && exp.member.exp.type == classExp)
9392             {
9393                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9394                typeKind = classType;
9395             }
9396
9397             if(id)
9398             {
9399                if(typeKind == intType || typeKind == enumType)
9400                   _class = eSystem_FindClass(privateModule, "int");
9401                else if(!_class)
9402                {
9403                   if(type.kind == classType && type._class && type._class.registered)
9404                   {
9405                      _class = type._class.registered;
9406                   }
9407                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9408                   {
9409                      _class = FindClass("char *").registered;
9410                   }
9411                   else if(type.kind == pointerType)
9412                   {
9413                      _class = eSystem_FindClass(privateModule, "uintptr");
9414                      FreeType(exp.expType);
9415                      exp.expType = ProcessTypeString("uintptr", false);
9416                      exp.byReference = true;
9417                   }
9418                   else
9419                   {
9420                      char string[1024] = "";
9421                      Symbol classSym;
9422                      PrintTypeNoConst(type, string, false, true);
9423                      classSym = FindClass(string);
9424                      if(classSym) _class = classSym.registered;
9425                   }
9426                }
9427             }
9428
9429             if(_class && id)
9430             {
9431                /*bool thisPtr =
9432                   (exp.member.exp.type == identifierExp &&
9433                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9434                Property prop = null;
9435                Method method = null;
9436                DataMember member = null;
9437                Property revConvert = null;
9438                ClassProperty classProp = null;
9439
9440                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9441                   exp.member.memberType = propertyMember;
9442
9443                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9444                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9445
9446                if(typeKind != subClassType)
9447                {
9448                   // Prioritize data members over properties for "this"
9449                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9450                   {
9451                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9452                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9453                      {
9454                         prop = eClass_FindProperty(_class, id.string, privateModule);
9455                         if(prop)
9456                            member = null;
9457                      }
9458                      if(!member && !prop)
9459                         prop = eClass_FindProperty(_class, id.string, privateModule);
9460                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9461                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9462                         exp.member.thisPtr = true;
9463                   }
9464                   // Prioritize properties over data members otherwise
9465                   else
9466                   {
9467                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9468                      if(!id.classSym)
9469                      {
9470                         prop = eClass_FindProperty(_class, id.string, null);
9471                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9472                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9473                      }
9474
9475                      if(!prop && !member)
9476                      {
9477                         method = eClass_FindMethod(_class, id.string, null);
9478                         if(!method)
9479                         {
9480                            prop = eClass_FindProperty(_class, id.string, privateModule);
9481                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9482                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9483                         }
9484                      }
9485
9486                      if(member && prop)
9487                      {
9488                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9489                            prop = null;
9490                         else
9491                            member = null;
9492                      }
9493                   }
9494                }
9495                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9496                   method = eClass_FindMethod(_class, id.string, privateModule);
9497                if(!prop && !member && !method)
9498                {
9499                   if(typeKind == subClassType)
9500                   {
9501                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9502                      if(classProp)
9503                      {
9504                         exp.member.memberType = classPropertyMember;
9505                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9506                      }
9507                      else
9508                      {
9509                         // Assume this is a class_data member
9510                         char structName[1024];
9511                         Identifier id = exp.member.member;
9512                         Expression classExp = exp.member.exp;
9513                         type.refCount++;
9514
9515                         FreeType(classExp.expType);
9516                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9517
9518                         strcpy(structName, "__ecereClassData_");
9519                         FullClassNameCat(structName, type._class.string, false);
9520                         exp.type = pointerExp;
9521                         exp.member.member = id;
9522
9523                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9524                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9525                               MkExpBrackets(MkListOne(MkExpOp(
9526                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9527                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9528                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9529                                  )));
9530
9531                         FreeType(type);
9532
9533                         ProcessExpressionType(exp);
9534                         return;
9535                      }
9536                   }
9537                   else
9538                   {
9539                      // Check for reverse conversion
9540                      // (Convert in an instantiation later, so that we can use
9541                      //  deep properties system)
9542                      Symbol classSym = FindClass(id.string);
9543                      if(classSym)
9544                      {
9545                         Class convertClass = classSym.registered;
9546                         if(convertClass)
9547                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9548                      }
9549                   }
9550                }
9551
9552                if(prop)
9553                {
9554                   exp.member.memberType = propertyMember;
9555                   if(!prop.dataType)
9556                      ProcessPropertyType(prop);
9557                   exp.expType = prop.dataType;
9558                   if(prop.dataType) prop.dataType.refCount++;
9559                }
9560                else if(member)
9561                {
9562                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9563                   {
9564                      FreeExpContents(exp);
9565                      exp.type = identifierExp;
9566                      exp.identifier = MkIdentifier("class");
9567                      ProcessExpressionType(exp);
9568                      return;
9569                   }
9570
9571                   exp.member.memberType = dataMember;
9572                   DeclareStruct(_class.fullName, false);
9573                   if(!member.dataType)
9574                   {
9575                      Context context = SetupTemplatesContext(_class);
9576                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9577                      FinishTemplatesContext(context);
9578                   }
9579                   exp.expType = member.dataType;
9580                   if(member.dataType) member.dataType.refCount++;
9581                }
9582                else if(revConvert)
9583                {
9584                   exp.member.memberType = reverseConversionMember;
9585                   exp.expType = MkClassType(revConvert._class.fullName);
9586                }
9587                else if(method)
9588                {
9589                   //if(inCompiler)
9590                   {
9591                      /*if(id._class)
9592                      {
9593                         exp.type = identifierExp;
9594                         exp.identifier = exp.member.member;
9595                      }
9596                      else*/
9597                         exp.member.memberType = methodMember;
9598                   }
9599                   if(!method.dataType)
9600                      ProcessMethodType(method);
9601                   exp.expType = Type
9602                   {
9603                      refCount = 1;
9604                      kind = methodType;
9605                      method = method;
9606                   };
9607
9608                   // Tricky spot here... To use instance versus class virtual table
9609                   // Put it back to what it was... What did we break?
9610
9611                   // Had to put it back for overriding Main of Thread global instance
9612
9613                   //exp.expType.methodClass = _class;
9614                   exp.expType.methodClass = (id && id._class) ? _class : null;
9615
9616                   // Need the actual class used for templated classes
9617                   exp.expType.usedClass = _class;
9618                }
9619                else if(!classProp)
9620                {
9621                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9622                   {
9623                      FreeExpContents(exp);
9624                      exp.type = identifierExp;
9625                      exp.identifier = MkIdentifier("class");
9626                      FreeType(exp.expType);
9627                      exp.expType = MkClassType("ecere::com::Class");
9628                      return;
9629                   }
9630                   yylloc = exp.member.member.loc;
9631                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9632                   if(inCompiler)
9633                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9634                }
9635
9636                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9637                {
9638                   Class tClass;
9639
9640                   tClass = _class;
9641                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9642
9643                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9644                   {
9645                      int id = 0;
9646                      ClassTemplateParameter curParam = null;
9647                      Class sClass;
9648
9649                      for(sClass = tClass; sClass; sClass = sClass.base)
9650                      {
9651                         id = 0;
9652                         if(sClass.templateClass) sClass = sClass.templateClass;
9653                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9654                         {
9655                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9656                            {
9657                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9658                                  id += sClass.templateParams.count;
9659                               break;
9660                            }
9661                            id++;
9662                         }
9663                         if(curParam) break;
9664                      }
9665
9666                      if(curParam && tClass.templateArgs[id].dataTypeString)
9667                      {
9668                         ClassTemplateArgument arg = tClass.templateArgs[id];
9669                         Context context = SetupTemplatesContext(tClass);
9670                         /*if(!arg.dataType)
9671                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9672                         FreeType(exp.expType);
9673                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9674                         if(exp.expType)
9675                         {
9676                            if(exp.expType.kind == thisClassType)
9677                            {
9678                               FreeType(exp.expType);
9679                               exp.expType = ReplaceThisClassType(_class);
9680                            }
9681
9682                            if(tClass.templateClass)
9683                               exp.expType.passAsTemplate = true;
9684                            //exp.expType.refCount++;
9685                            if(!exp.destType)
9686                            {
9687                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9688                               //exp.destType.refCount++;
9689
9690                               if(exp.destType.kind == thisClassType)
9691                               {
9692                                  FreeType(exp.destType);
9693                                  exp.destType = ReplaceThisClassType(_class);
9694                               }
9695                            }
9696                         }
9697                         FinishTemplatesContext(context);
9698                      }
9699                   }
9700                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9701                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9702                   {
9703                      int id = 0;
9704                      ClassTemplateParameter curParam = null;
9705                      Class sClass;
9706
9707                      for(sClass = tClass; sClass; sClass = sClass.base)
9708                      {
9709                         id = 0;
9710                         if(sClass.templateClass) sClass = sClass.templateClass;
9711                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9712                         {
9713                            if(curParam.type == TemplateParameterType::type &&
9714                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9715                            {
9716                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9717                                  id += sClass.templateParams.count;
9718                               break;
9719                            }
9720                            id++;
9721                         }
9722                         if(curParam) break;
9723                      }
9724
9725                      if(curParam)
9726                      {
9727                         ClassTemplateArgument arg = tClass.templateArgs[id];
9728                         Context context = SetupTemplatesContext(tClass);
9729                         Type basicType;
9730                         /*if(!arg.dataType)
9731                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9732
9733                         basicType = ProcessTypeString(arg.dataTypeString, false);
9734                         if(basicType)
9735                         {
9736                            if(basicType.kind == thisClassType)
9737                            {
9738                               FreeType(basicType);
9739                               basicType = ReplaceThisClassType(_class);
9740                            }
9741
9742                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9743                            if(tClass.templateClass)
9744                               basicType.passAsTemplate = true;
9745                            */
9746
9747                            FreeType(exp.expType);
9748
9749                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9750                            //exp.expType.refCount++;
9751                            if(!exp.destType)
9752                            {
9753                               exp.destType = exp.expType;
9754                               exp.destType.refCount++;
9755                            }
9756
9757                            {
9758                               Expression newExp { };
9759                               OldList * specs = MkList();
9760                               Declarator decl;
9761                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
9762                               *newExp = *exp;
9763                               if(exp.destType) exp.destType.refCount++;
9764                               if(exp.expType)  exp.expType.refCount++;
9765                               exp.type = castExp;
9766                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
9767                               exp.cast.exp = newExp;
9768                               //FreeType(exp.expType);
9769                               //exp.expType = null;
9770                               //ProcessExpressionType(sourceExp);
9771                            }
9772                         }
9773                         FinishTemplatesContext(context);
9774                      }
9775                   }
9776                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
9777                   {
9778                      Class expClass = exp.expType._class.registered;
9779                      if(expClass)
9780                      {
9781                         Class cClass = null;
9782                         int c;
9783                         int p = 0;
9784                         int paramCount = 0;
9785                         int lastParam = -1;
9786                         char templateString[1024];
9787                         ClassTemplateParameter param;
9788                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
9789                         while(cClass != expClass)
9790                         {
9791                            Class sClass;
9792                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
9793                            cClass = sClass;
9794
9795                            for(param = cClass.templateParams.first; param; param = param.next)
9796                            {
9797                               Class cClassCur = null;
9798                               int c;
9799                               int cp = 0;
9800                               ClassTemplateParameter paramCur = null;
9801                               ClassTemplateArgument arg;
9802                               while(cClassCur != tClass && !paramCur)
9803                               {
9804                                  Class sClassCur;
9805                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
9806                                  cClassCur = sClassCur;
9807
9808                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
9809                                  {
9810                                     if(!strcmp(paramCur.name, param.name))
9811                                     {
9812
9813                                        break;
9814                                     }
9815                                     cp++;
9816                                  }
9817                               }
9818                               if(paramCur && paramCur.type == TemplateParameterType::type)
9819                                  arg = tClass.templateArgs[cp];
9820                               else
9821                                  arg = expClass.templateArgs[p];
9822
9823                               {
9824                                  char argument[256];
9825                                  argument[0] = '\0';
9826                                  /*if(arg.name)
9827                                  {
9828                                     strcat(argument, arg.name.string);
9829                                     strcat(argument, " = ");
9830                                  }*/
9831                                  switch(param.type)
9832                                  {
9833                                     case expression:
9834                                     {
9835                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9836                                        char expString[1024];
9837                                        OldList * specs = MkList();
9838                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9839                                        Expression exp;
9840                                        char * string = PrintHexUInt64(arg.expression.ui64);
9841                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9842                                        delete string;
9843
9844                                        ProcessExpressionType(exp);
9845                                        ComputeExpression(exp);
9846                                        expString[0] = '\0';
9847                                        PrintExpression(exp, expString);
9848                                        strcat(argument, expString);
9849                                        // delete exp;
9850                                        FreeExpression(exp);
9851                                        break;
9852                                     }
9853                                     case identifier:
9854                                     {
9855                                        strcat(argument, arg.member.name);
9856                                        break;
9857                                     }
9858                                     case TemplateParameterType::type:
9859                                     {
9860                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9861                                           strcat(argument, arg.dataTypeString);
9862                                        break;
9863                                     }
9864                                  }
9865                                  if(argument[0])
9866                                  {
9867                                     if(paramCount) strcat(templateString, ", ");
9868                                     if(lastParam != p - 1)
9869                                     {
9870                                        strcat(templateString, param.name);
9871                                        strcat(templateString, " = ");
9872                                     }
9873                                     strcat(templateString, argument);
9874                                     paramCount++;
9875                                     lastParam = p;
9876                                  }
9877                               }
9878                               p++;
9879                            }
9880                         }
9881                         {
9882                            int len = strlen(templateString);
9883                            if(templateString[len-1] == '>') templateString[len++] = ' ';
9884                            templateString[len++] = '>';
9885                            templateString[len++] = '\0';
9886                         }
9887
9888                         FreeType(exp.expType);
9889                         {
9890                            Context context = SetupTemplatesContext(tClass);
9891                            exp.expType = ProcessTypeString(templateString, false);
9892                            FinishTemplatesContext(context);
9893                         }
9894                      }
9895                   }
9896                }
9897             }
9898             else
9899                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
9900          }
9901          else if(type && (type.kind == structType || type.kind == unionType))
9902          {
9903             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
9904             if(memberType)
9905             {
9906                exp.expType = memberType;
9907                if(memberType)
9908                   memberType.refCount++;
9909             }
9910          }
9911          else
9912          {
9913             char expString[10240];
9914             expString[0] = '\0';
9915             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
9916             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
9917          }
9918
9919          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
9920          {
9921             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
9922             {
9923                Identifier id = exp.member.member;
9924                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9925                if(_class)
9926                {
9927                   FreeType(exp.expType);
9928                   exp.expType = ReplaceThisClassType(_class);
9929                }
9930             }
9931          }
9932          yylloc = oldyylloc;
9933          break;
9934       }
9935       // Convert x->y into (*x).y
9936       case pointerExp:
9937       {
9938          Type destType = exp.destType;
9939
9940          // DOING THIS LATER NOW...
9941          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9942          {
9943             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9944             /* TODO: Name Space Fix ups
9945             if(!exp.member.member.classSym)
9946                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
9947             */
9948          }
9949
9950          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
9951          exp.type = memberExp;
9952          if(destType)
9953             destType.count++;
9954          ProcessExpressionType(exp);
9955          if(destType)
9956             destType.count--;
9957          break;
9958       }
9959       case classSizeExp:
9960       {
9961          //ComputeExpression(exp);
9962
9963          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
9964          if(classSym && classSym.registered)
9965          {
9966             if(classSym.registered.type == noHeadClass)
9967             {
9968                char name[1024];
9969                name[0] = '\0';
9970                DeclareStruct(classSym.string, false);
9971                FreeSpecifier(exp._class);
9972                exp.type = typeSizeExp;
9973                FullClassNameCat(name, classSym.string, false);
9974                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
9975             }
9976             else
9977             {
9978                if(classSym.registered.fixed)
9979                {
9980                   FreeSpecifier(exp._class);
9981                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
9982                   exp.type = constantExp;
9983                }
9984                else
9985                {
9986                   char className[1024];
9987                   strcpy(className, "__ecereClass_");
9988                   FullClassNameCat(className, classSym.string, true);
9989                   MangleClassName(className);
9990
9991                   DeclareClass(classSym, className);
9992
9993                   FreeExpContents(exp);
9994                   exp.type = pointerExp;
9995                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
9996                   exp.member.member = MkIdentifier("structSize");
9997                }
9998             }
9999          }
10000
10001          exp.expType = Type
10002          {
10003             refCount = 1;
10004             kind = intType;
10005          };
10006          // exp.isConstant = true;
10007          break;
10008       }
10009       case typeSizeExp:
10010       {
10011          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10012
10013          exp.expType = Type
10014          {
10015             refCount = 1;
10016             kind = intType;
10017          };
10018          exp.isConstant = true;
10019
10020          DeclareType(type, false, false);
10021          FreeType(type);
10022          break;
10023       }
10024       case castExp:
10025       {
10026          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10027          type.count = 1;
10028          FreeType(exp.cast.exp.destType);
10029          exp.cast.exp.destType = type;
10030          type.refCount++;
10031          ProcessExpressionType(exp.cast.exp);
10032          type.count = 0;
10033          exp.expType = type;
10034          //type.refCount++;
10035
10036          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10037          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10038          {
10039             void * prev = exp.prev, * next = exp.next;
10040             Type expType = exp.cast.exp.destType;
10041             Expression castExp = exp.cast.exp;
10042             Type destType = exp.destType;
10043
10044             if(expType) expType.refCount++;
10045
10046             //FreeType(exp.destType);
10047             FreeType(exp.expType);
10048             FreeTypeName(exp.cast.typeName);
10049
10050             *exp = *castExp;
10051             FreeType(exp.expType);
10052             FreeType(exp.destType);
10053
10054             exp.expType = expType;
10055             exp.destType = destType;
10056
10057             delete castExp;
10058
10059             exp.prev = prev;
10060             exp.next = next;
10061
10062          }
10063          else
10064          {
10065             exp.isConstant = exp.cast.exp.isConstant;
10066          }
10067          //FreeType(type);
10068          break;
10069       }
10070       case extensionInitializerExp:
10071       {
10072          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10073          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10074          // ProcessInitializer(exp.initializer.initializer, type);
10075          exp.expType = type;
10076          break;
10077       }
10078       case vaArgExp:
10079       {
10080          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10081          ProcessExpressionType(exp.vaArg.exp);
10082          exp.expType = type;
10083          break;
10084       }
10085       case conditionExp:
10086       {
10087          Expression e;
10088          exp.isConstant = true;
10089
10090          FreeType(exp.cond.cond.destType);
10091          exp.cond.cond.destType = MkClassType("bool");
10092          exp.cond.cond.destType.truth = true;
10093          ProcessExpressionType(exp.cond.cond);
10094          if(!exp.cond.cond.isConstant)
10095             exp.isConstant = false;
10096          for(e = exp.cond.exp->first; e; e = e.next)
10097          {
10098             if(!e.next)
10099             {
10100                FreeType(e.destType);
10101                e.destType = exp.destType;
10102                if(e.destType) e.destType.refCount++;
10103             }
10104             ProcessExpressionType(e);
10105             if(!e.next)
10106             {
10107                exp.expType = e.expType;
10108                if(e.expType) e.expType.refCount++;
10109             }
10110             if(!e.isConstant)
10111                exp.isConstant = false;
10112          }
10113
10114          FreeType(exp.cond.elseExp.destType);
10115          // Added this check if we failed to find an expType
10116          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10117
10118          // Reversed it...
10119          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10120
10121          if(exp.cond.elseExp.destType)
10122             exp.cond.elseExp.destType.refCount++;
10123          ProcessExpressionType(exp.cond.elseExp);
10124
10125          // FIXED THIS: Was done before calling process on elseExp
10126          if(!exp.cond.elseExp.isConstant)
10127             exp.isConstant = false;
10128          break;
10129       }
10130       case extensionCompoundExp:
10131       {
10132          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10133          {
10134             Statement last = exp.compound.compound.statements->last;
10135             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10136             {
10137                ((Expression)last.expressions->last).destType = exp.destType;
10138                if(exp.destType)
10139                   exp.destType.refCount++;
10140             }
10141             ProcessStatement(exp.compound);
10142             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10143             if(exp.expType)
10144                exp.expType.refCount++;
10145          }
10146          break;
10147       }
10148       case classExp:
10149       {
10150          Specifier spec = exp._classExp.specifiers->first;
10151          if(spec && spec.type == nameSpecifier)
10152          {
10153             exp.expType = MkClassType(spec.name);
10154             exp.expType.kind = subClassType;
10155             exp.byReference = true;
10156          }
10157          else
10158          {
10159             exp.expType = MkClassType("ecere::com::Class");
10160             exp.byReference = true;
10161          }
10162          break;
10163       }
10164       case classDataExp:
10165       {
10166          Class _class = thisClass ? thisClass : currentClass;
10167          if(_class)
10168          {
10169             Identifier id = exp.classData.id;
10170             char structName[1024];
10171             Expression classExp;
10172             strcpy(structName, "__ecereClassData_");
10173             FullClassNameCat(structName, _class.fullName, false);
10174             exp.type = pointerExp;
10175             exp.member.member = id;
10176             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10177                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10178             else
10179                classExp = MkExpIdentifier(MkIdentifier("class"));
10180
10181             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10182                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10183                   MkExpBrackets(MkListOne(MkExpOp(
10184                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10185                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10186                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10187                      )));
10188
10189             ProcessExpressionType(exp);
10190             return;
10191          }
10192          break;
10193       }
10194       case arrayExp:
10195       {
10196          Type type = null;
10197          char * typeString = null;
10198          char typeStringBuf[1024];
10199          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10200             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10201          {
10202             Class templateClass = exp.destType._class.registered;
10203             typeString = templateClass.templateArgs[2].dataTypeString;
10204          }
10205          else if(exp.list)
10206          {
10207             // Guess type from expressions in the array
10208             Expression e;
10209             for(e = exp.list->first; e; e = e.next)
10210             {
10211                ProcessExpressionType(e);
10212                if(e.expType)
10213                {
10214                   if(!type) { type = e.expType; type.refCount++; }
10215                   else
10216                   {
10217                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10218                      if(!MatchTypeExpression(e, type, null, false))
10219                      {
10220                         FreeType(type);
10221                         type = e.expType;
10222                         e.expType = null;
10223
10224                         e = exp.list->first;
10225                         ProcessExpressionType(e);
10226                         if(e.expType)
10227                         {
10228                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10229                            if(!MatchTypeExpression(e, type, null, false))
10230                            {
10231                               FreeType(e.expType);
10232                               e.expType = null;
10233                               FreeType(type);
10234                               type = null;
10235                               break;
10236                            }
10237                         }
10238                      }
10239                   }
10240                   if(e.expType)
10241                   {
10242                      FreeType(e.expType);
10243                      e.expType = null;
10244                   }
10245                }
10246             }
10247             if(type)
10248             {
10249                typeStringBuf[0] = '\0';
10250                PrintTypeNoConst(type, typeStringBuf, false, true);
10251                typeString = typeStringBuf;
10252                FreeType(type);
10253                type = null;
10254             }
10255          }
10256          if(typeString)
10257          {
10258             /*
10259             (Container)& (struct BuiltInContainer)
10260             {
10261                ._vTbl = class(BuiltInContainer)._vTbl,
10262                ._class = class(BuiltInContainer),
10263                .refCount = 0,
10264                .data = (int[]){ 1, 7, 3, 4, 5 },
10265                .count = 5,
10266                .type = class(int),
10267             }
10268             */
10269             char templateString[1024];
10270             OldList * initializers = MkList();
10271             OldList * structInitializers = MkList();
10272             OldList * specs = MkList();
10273             Expression expExt;
10274             Declarator decl = SpecDeclFromString(typeString, specs, null);
10275             sprintf(templateString, "Container<%s>", typeString);
10276
10277             if(exp.list)
10278             {
10279                Expression e;
10280                type = ProcessTypeString(typeString, false);
10281                while(e = exp.list->first)
10282                {
10283                   exp.list->Remove(e);
10284                   e.destType = type;
10285                   type.refCount++;
10286                   ProcessExpressionType(e);
10287                   ListAdd(initializers, MkInitializerAssignment(e));
10288                }
10289                FreeType(type);
10290                delete exp.list;
10291             }
10292
10293             DeclareStruct("ecere::com::BuiltInContainer", false);
10294
10295             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10296                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10297             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10298                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10299             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10300                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10301             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10302                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10303                MkInitializerList(initializers))));
10304                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10305             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10306                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10307             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10308                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10309             exp.expType = ProcessTypeString(templateString, false);
10310             exp.type = bracketsExp;
10311             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10312                MkExpOp(null, '&',
10313                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10314                   MkInitializerList(structInitializers)))));
10315             ProcessExpressionType(expExt);
10316          }
10317          else
10318          {
10319             exp.expType = ProcessTypeString("Container", false);
10320             Compiler_Error($"Couldn't determine type of array elements\n");
10321          }
10322          break;
10323       }
10324    }
10325
10326    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10327    {
10328       FreeType(exp.expType);
10329       exp.expType = ReplaceThisClassType(thisClass);
10330    }
10331
10332    // Resolve structures here
10333    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10334    {
10335       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10336       // TODO: Fix members reference...
10337       if(symbol)
10338       {
10339          if(exp.expType.kind != enumType)
10340          {
10341             Type member;
10342             String enumName = CopyString(exp.expType.enumName);
10343
10344             // Fixed a memory leak on self-referencing C structs typedefs
10345             // by instantiating a new type rather than simply copying members
10346             // into exp.expType
10347             FreeType(exp.expType);
10348             exp.expType = Type { };
10349             exp.expType.kind = symbol.type.kind;
10350             exp.expType.refCount++;
10351             exp.expType.enumName = enumName;
10352
10353             exp.expType.members = symbol.type.members;
10354             for(member = symbol.type.members.first; member; member = member.next)
10355                member.refCount++;
10356          }
10357          else
10358          {
10359             NamedLink member;
10360             for(member = symbol.type.members.first; member; member = member.next)
10361             {
10362                NamedLink value { name = CopyString(member.name) };
10363                exp.expType.members.Add(value);
10364             }
10365          }
10366       }
10367    }
10368
10369    yylloc = exp.loc;
10370    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10371    else if(exp.destType && !exp.destType.keepCast)
10372    {
10373       if(!CheckExpressionType(exp, exp.destType, false))
10374       {
10375          if(!exp.destType.count || unresolved)
10376          {
10377             if(!exp.expType)
10378             {
10379                yylloc = exp.loc;
10380                if(exp.destType.kind != ellipsisType)
10381                {
10382                   char type2[1024];
10383                   type2[0] = '\0';
10384                   if(inCompiler)
10385                   {
10386                      char expString[10240];
10387                      expString[0] = '\0';
10388
10389                      PrintType(exp.destType, type2, false, true);
10390
10391                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10392                      if(unresolved)
10393                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10394                      else if(exp.type != dummyExp)
10395                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10396                   }
10397                }
10398                else
10399                {
10400                   char expString[10240] ;
10401                   expString[0] = '\0';
10402                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10403
10404                   if(unresolved)
10405                      Compiler_Error($"unresolved identifier %s\n", expString);
10406                   else if(exp.type != dummyExp)
10407                      Compiler_Error($"couldn't determine type of %s\n", expString);
10408                }
10409             }
10410             else
10411             {
10412                char type1[1024];
10413                char type2[1024];
10414                type1[0] = '\0';
10415                type2[0] = '\0';
10416                if(inCompiler)
10417                {
10418                   PrintType(exp.expType, type1, false, true);
10419                   PrintType(exp.destType, type2, false, true);
10420                }
10421
10422                //CheckExpressionType(exp, exp.destType, false);
10423
10424                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10425                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10426                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10427                else
10428                {
10429                   char expString[10240];
10430                   expString[0] = '\0';
10431                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10432
10433 #ifdef _DEBUG
10434                   CheckExpressionType(exp, exp.destType, false);
10435 #endif
10436                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10437                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10438                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10439
10440                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10441                   FreeType(exp.expType);
10442                   exp.destType.refCount++;
10443                   exp.expType = exp.destType;
10444                }
10445             }
10446          }
10447       }
10448       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10449       {
10450          Expression newExp { };
10451          char typeString[1024];
10452          OldList * specs = MkList();
10453          Declarator decl;
10454
10455          typeString[0] = '\0';
10456
10457          *newExp = *exp;
10458
10459          if(exp.expType)  exp.expType.refCount++;
10460          if(exp.expType)  exp.expType.refCount++;
10461          exp.type = castExp;
10462          newExp.destType = exp.expType;
10463
10464          PrintType(exp.expType, typeString, false, false);
10465          decl = SpecDeclFromString(typeString, specs, null);
10466
10467          exp.cast.typeName = MkTypeName(specs, decl);
10468          exp.cast.exp = newExp;
10469       }
10470    }
10471    else if(unresolved)
10472    {
10473       if(exp.identifier._class && exp.identifier._class.name)
10474          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10475       else if(exp.identifier.string && exp.identifier.string[0])
10476          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10477    }
10478    else if(!exp.expType && exp.type != dummyExp)
10479    {
10480       char expString[10240];
10481       expString[0] = '\0';
10482       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10483       Compiler_Error($"couldn't determine type of %s\n", expString);
10484    }
10485
10486    // Let's try to support any_object & typed_object here:
10487    if(inCompiler)
10488       ApplyAnyObjectLogic(exp);
10489
10490    // Mark nohead classes as by reference, unless we're casting them to an integral type
10491    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10492       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10493          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10494           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10495    {
10496       exp.byReference = true;
10497    }
10498    yylloc = oldyylloc;
10499 }
10500
10501 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10502 {
10503    // THIS CODE WILL FIND NEXT MEMBER...
10504    if(*curMember)
10505    {
10506       *curMember = (*curMember).next;
10507
10508       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10509       {
10510          *curMember = subMemberStack[--(*subMemberStackPos)];
10511          *curMember = (*curMember).next;
10512       }
10513
10514       // SKIP ALL PROPERTIES HERE...
10515       while((*curMember) && (*curMember).isProperty)
10516          *curMember = (*curMember).next;
10517
10518       if(subMemberStackPos)
10519       {
10520          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10521          {
10522             subMemberStack[(*subMemberStackPos)++] = *curMember;
10523
10524             *curMember = (*curMember).members.first;
10525             while(*curMember && (*curMember).isProperty)
10526                *curMember = (*curMember).next;
10527          }
10528       }
10529    }
10530    while(!*curMember)
10531    {
10532       if(!*curMember)
10533       {
10534          if(subMemberStackPos && *subMemberStackPos)
10535          {
10536             *curMember = subMemberStack[--(*subMemberStackPos)];
10537             *curMember = (*curMember).next;
10538          }
10539          else
10540          {
10541             Class lastCurClass = *curClass;
10542
10543             if(*curClass == _class) break;     // REACHED THE END
10544
10545             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10546             *curMember = (*curClass).membersAndProperties.first;
10547          }
10548
10549          while((*curMember) && (*curMember).isProperty)
10550             *curMember = (*curMember).next;
10551          if(subMemberStackPos)
10552          {
10553             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10554             {
10555                subMemberStack[(*subMemberStackPos)++] = *curMember;
10556
10557                *curMember = (*curMember).members.first;
10558                while(*curMember && (*curMember).isProperty)
10559                   *curMember = (*curMember).next;
10560             }
10561          }
10562       }
10563    }
10564 }
10565
10566
10567 static void ProcessInitializer(Initializer init, Type type)
10568 {
10569    switch(init.type)
10570    {
10571       case expInitializer:
10572          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10573          {
10574             // TESTING THIS FOR SHUTTING = 0 WARNING
10575             if(init.exp && !init.exp.destType)
10576             {
10577                FreeType(init.exp.destType);
10578                init.exp.destType = type;
10579                if(type) type.refCount++;
10580             }
10581             if(init.exp)
10582             {
10583                ProcessExpressionType(init.exp);
10584                init.isConstant = init.exp.isConstant;
10585             }
10586             break;
10587          }
10588          else
10589          {
10590             Expression exp = init.exp;
10591             Instantiation inst = exp.instance;
10592             MembersInit members;
10593
10594             init.type = listInitializer;
10595             init.list = MkList();
10596
10597             if(inst.members)
10598             {
10599                for(members = inst.members->first; members; members = members.next)
10600                {
10601                   if(members.type == dataMembersInit)
10602                   {
10603                      MemberInit member;
10604                      for(member = members.dataMembers->first; member; member = member.next)
10605                      {
10606                         ListAdd(init.list, member.initializer);
10607                         member.initializer = null;
10608                      }
10609                   }
10610                   // Discard all MembersInitMethod
10611                }
10612             }
10613             FreeExpression(exp);
10614          }
10615       case listInitializer:
10616       {
10617          Initializer i;
10618          Type initializerType = null;
10619          Class curClass = null;
10620          DataMember curMember = null;
10621          DataMember subMemberStack[256];
10622          int subMemberStackPos = 0;
10623
10624          if(type && type.kind == arrayType)
10625             initializerType = Dereference(type);
10626          else if(type && (type.kind == structType || type.kind == unionType))
10627             initializerType = type.members.first;
10628
10629          for(i = init.list->first; i; i = i.next)
10630          {
10631             if(type && type.kind == classType && type._class && type._class.registered)
10632             {
10633                // 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)
10634                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10635                // TODO: Generate error on initializing a private data member this way from another module...
10636                if(curMember)
10637                {
10638                   if(!curMember.dataType)
10639                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10640                   initializerType = curMember.dataType;
10641                }
10642             }
10643             ProcessInitializer(i, initializerType);
10644             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10645                initializerType = initializerType.next;
10646             if(!i.isConstant)
10647                init.isConstant = false;
10648          }
10649
10650          if(type && type.kind == arrayType)
10651             FreeType(initializerType);
10652
10653          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10654          {
10655             Compiler_Error($"Assigning list initializer to non list\n");
10656          }
10657          break;
10658       }
10659    }
10660 }
10661
10662 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10663 {
10664    switch(spec.type)
10665    {
10666       case baseSpecifier:
10667       {
10668          if(spec.specifier == THISCLASS)
10669          {
10670             if(thisClass)
10671             {
10672                spec.type = nameSpecifier;
10673                spec.name = ReplaceThisClass(thisClass);
10674                spec.symbol = FindClass(spec.name);
10675                ProcessSpecifier(spec, declareStruct);
10676             }
10677          }
10678          break;
10679       }
10680       case nameSpecifier:
10681       {
10682          Symbol symbol = FindType(curContext, spec.name);
10683          if(symbol)
10684             DeclareType(symbol.type, true, true);
10685          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10686             DeclareStruct(spec.name, false);
10687          break;
10688       }
10689       case enumSpecifier:
10690       {
10691          Enumerator e;
10692          if(spec.list)
10693          {
10694             for(e = spec.list->first; e; e = e.next)
10695             {
10696                if(e.exp)
10697                   ProcessExpressionType(e.exp);
10698             }
10699          }
10700          break;
10701       }
10702       case structSpecifier:
10703       case unionSpecifier:
10704       {
10705          if(spec.definitions)
10706          {
10707             ClassDef def;
10708             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10709             //if(symbol)
10710                ProcessClass(spec.definitions, symbol);
10711             /*else
10712             {
10713                for(def = spec.definitions->first; def; def = def.next)
10714                {
10715                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10716                      ProcessDeclaration(def.decl);
10717                }
10718             }*/
10719          }
10720          break;
10721       }
10722       /*
10723       case classSpecifier:
10724       {
10725          Symbol classSym = FindClass(spec.name);
10726          if(classSym && classSym.registered && classSym.registered.type == structClass)
10727             DeclareStruct(spec.name, false);
10728          break;
10729       }
10730       */
10731    }
10732 }
10733
10734
10735 static void ProcessDeclarator(Declarator decl)
10736 {
10737    switch(decl.type)
10738    {
10739       case identifierDeclarator:
10740          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10741          {
10742             FreeSpecifier(decl.identifier._class);
10743             decl.identifier._class = null;
10744          }
10745          break;
10746       case arrayDeclarator:
10747          if(decl.array.exp)
10748             ProcessExpressionType(decl.array.exp);
10749       case structDeclarator:
10750       case bracketsDeclarator:
10751       case functionDeclarator:
10752       case pointerDeclarator:
10753       case extendedDeclarator:
10754       case extendedDeclaratorEnd:
10755          if(decl.declarator)
10756             ProcessDeclarator(decl.declarator);
10757          if(decl.type == functionDeclarator)
10758          {
10759             Identifier id = GetDeclId(decl);
10760             if(id && id._class)
10761             {
10762                TypeName param
10763                {
10764                   qualifiers = MkListOne(id._class);
10765                   declarator = null;
10766                };
10767                if(!decl.function.parameters)
10768                   decl.function.parameters = MkList();
10769                decl.function.parameters->Insert(null, param);
10770                id._class = null;
10771             }
10772             if(decl.function.parameters)
10773             {
10774                TypeName param;
10775
10776                for(param = decl.function.parameters->first; param; param = param.next)
10777                {
10778                   if(param.qualifiers && param.qualifiers->first)
10779                   {
10780                      Specifier spec = param.qualifiers->first;
10781                      if(spec && spec.specifier == TYPED_OBJECT)
10782                      {
10783                         Declarator d = param.declarator;
10784                         TypeName newParam
10785                         {
10786                            qualifiers = MkListOne(MkSpecifier(VOID));
10787                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10788                         };
10789
10790                         FreeList(param.qualifiers, FreeSpecifier);
10791
10792                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
10793                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
10794
10795                         decl.function.parameters->Insert(param, newParam);
10796                         param = newParam;
10797                      }
10798                      else if(spec && spec.specifier == ANY_OBJECT)
10799                      {
10800                         Declarator d = param.declarator;
10801
10802                         FreeList(param.qualifiers, FreeSpecifier);
10803
10804                         param.qualifiers = MkListOne(MkSpecifier(VOID));
10805                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10806                      }
10807                      else if(spec.specifier == THISCLASS)
10808                      {
10809                         if(thisClass)
10810                         {
10811                            spec.type = nameSpecifier;
10812                            spec.name = ReplaceThisClass(thisClass);
10813                            spec.symbol = FindClass(spec.name);
10814                            ProcessSpecifier(spec, false);
10815                         }
10816                      }
10817                   }
10818
10819                   if(param.declarator)
10820                      ProcessDeclarator(param.declarator);
10821                }
10822             }
10823          }
10824          break;
10825    }
10826 }
10827
10828 static void ProcessDeclaration(Declaration decl)
10829 {
10830    yylloc = decl.loc;
10831    switch(decl.type)
10832    {
10833       case initDeclaration:
10834       {
10835          bool declareStruct = false;
10836          /*
10837          lineNum = decl.pos.line;
10838          column = decl.pos.col;
10839          */
10840
10841          if(decl.declarators)
10842          {
10843             InitDeclarator d;
10844
10845             for(d = decl.declarators->first; d; d = d.next)
10846             {
10847                Type type, subType;
10848                ProcessDeclarator(d.declarator);
10849
10850                type = ProcessType(decl.specifiers, d.declarator);
10851
10852                if(d.initializer)
10853                {
10854                   ProcessInitializer(d.initializer, type);
10855
10856                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
10857
10858                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
10859                      d.initializer.exp.type == instanceExp)
10860                   {
10861                      if(type.kind == classType && type._class ==
10862                         d.initializer.exp.expType._class)
10863                      {
10864                         Instantiation inst = d.initializer.exp.instance;
10865                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
10866
10867                         d.initializer.exp.instance = null;
10868                         if(decl.specifiers)
10869                            FreeList(decl.specifiers, FreeSpecifier);
10870                         FreeList(decl.declarators, FreeInitDeclarator);
10871
10872                         d = null;
10873
10874                         decl.type = instDeclaration;
10875                         decl.inst = inst;
10876                      }
10877                   }
10878                }
10879                for(subType = type; subType;)
10880                {
10881                   if(subType.kind == classType)
10882                   {
10883                      declareStruct = true;
10884                      break;
10885                   }
10886                   else if(subType.kind == pointerType)
10887                      break;
10888                   else if(subType.kind == arrayType)
10889                      subType = subType.arrayType;
10890                   else
10891                      break;
10892                }
10893
10894                FreeType(type);
10895                if(!d) break;
10896             }
10897          }
10898
10899          if(decl.specifiers)
10900          {
10901             Specifier s;
10902             for(s = decl.specifiers->first; s; s = s.next)
10903             {
10904                ProcessSpecifier(s, declareStruct);
10905             }
10906          }
10907          break;
10908       }
10909       case instDeclaration:
10910       {
10911          ProcessInstantiationType(decl.inst);
10912          break;
10913       }
10914       case structDeclaration:
10915       {
10916          Specifier spec;
10917          Declarator d;
10918          bool declareStruct = false;
10919
10920          if(decl.declarators)
10921          {
10922             for(d = decl.declarators->first; d; d = d.next)
10923             {
10924                Type type = ProcessType(decl.specifiers, d.declarator);
10925                Type subType;
10926                ProcessDeclarator(d);
10927                for(subType = type; subType;)
10928                {
10929                   if(subType.kind == classType)
10930                   {
10931                      declareStruct = true;
10932                      break;
10933                   }
10934                   else if(subType.kind == pointerType)
10935                      break;
10936                   else if(subType.kind == arrayType)
10937                      subType = subType.arrayType;
10938                   else
10939                      break;
10940                }
10941                FreeType(type);
10942             }
10943          }
10944          if(decl.specifiers)
10945          {
10946             for(spec = decl.specifiers->first; spec; spec = spec.next)
10947                ProcessSpecifier(spec, declareStruct);
10948          }
10949          break;
10950       }
10951    }
10952 }
10953
10954 static FunctionDefinition curFunction;
10955
10956 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
10957 {
10958    char propName[1024], propNameM[1024];
10959    char getName[1024], setName[1024];
10960    OldList * args;
10961
10962    DeclareProperty(prop, setName, getName);
10963
10964    // eInstance_FireWatchers(object, prop);
10965    strcpy(propName, "__ecereProp_");
10966    FullClassNameCat(propName, prop._class.fullName, false);
10967    strcat(propName, "_");
10968    // strcat(propName, prop.name);
10969    FullClassNameCat(propName, prop.name, true);
10970    MangleClassName(propName);
10971
10972    strcpy(propNameM, "__ecerePropM_");
10973    FullClassNameCat(propNameM, prop._class.fullName, false);
10974    strcat(propNameM, "_");
10975    // strcat(propNameM, prop.name);
10976    FullClassNameCat(propNameM, prop.name, true);
10977    MangleClassName(propNameM);
10978
10979    if(prop.isWatchable)
10980    {
10981       args = MkList();
10982       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10983       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10984       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10985
10986       args = MkList();
10987       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10988       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
10989       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
10990    }
10991
10992
10993    {
10994       args = MkList();
10995       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
10996       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
10997       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
10998
10999       args = MkList();
11000       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11001       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11002       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11003    }
11004
11005    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11006       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11007       curFunction.propSet.fireWatchersDone = true;
11008 }
11009
11010 static void ProcessStatement(Statement stmt)
11011 {
11012    yylloc = stmt.loc;
11013    /*
11014    lineNum = stmt.pos.line;
11015    column = stmt.pos.col;
11016    */
11017    switch(stmt.type)
11018    {
11019       case labeledStmt:
11020          ProcessStatement(stmt.labeled.stmt);
11021          break;
11022       case caseStmt:
11023          // This expression should be constant...
11024          if(stmt.caseStmt.exp)
11025          {
11026             FreeType(stmt.caseStmt.exp.destType);
11027             stmt.caseStmt.exp.destType = curSwitchType;
11028             if(curSwitchType) curSwitchType.refCount++;
11029             ProcessExpressionType(stmt.caseStmt.exp);
11030             ComputeExpression(stmt.caseStmt.exp);
11031          }
11032          if(stmt.caseStmt.stmt)
11033             ProcessStatement(stmt.caseStmt.stmt);
11034          break;
11035       case compoundStmt:
11036       {
11037          if(stmt.compound.context)
11038          {
11039             Declaration decl;
11040             Statement s;
11041
11042             Statement prevCompound = curCompound;
11043             Context prevContext = curContext;
11044
11045             if(!stmt.compound.isSwitch)
11046                curCompound = stmt;
11047             curContext = stmt.compound.context;
11048
11049             if(stmt.compound.declarations)
11050             {
11051                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11052                   ProcessDeclaration(decl);
11053             }
11054             if(stmt.compound.statements)
11055             {
11056                for(s = stmt.compound.statements->first; s; s = s.next)
11057                   ProcessStatement(s);
11058             }
11059
11060             curContext = prevContext;
11061             curCompound = prevCompound;
11062          }
11063          break;
11064       }
11065       case expressionStmt:
11066       {
11067          Expression exp;
11068          if(stmt.expressions)
11069          {
11070             for(exp = stmt.expressions->first; exp; exp = exp.next)
11071                ProcessExpressionType(exp);
11072          }
11073          break;
11074       }
11075       case ifStmt:
11076       {
11077          Expression exp;
11078
11079          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11080          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11081          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11082          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11083          {
11084             ProcessExpressionType(exp);
11085          }
11086          if(stmt.ifStmt.stmt)
11087             ProcessStatement(stmt.ifStmt.stmt);
11088          if(stmt.ifStmt.elseStmt)
11089             ProcessStatement(stmt.ifStmt.elseStmt);
11090          break;
11091       }
11092       case switchStmt:
11093       {
11094          Type oldSwitchType = curSwitchType;
11095          if(stmt.switchStmt.exp)
11096          {
11097             Expression exp;
11098             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11099             {
11100                if(!exp.next)
11101                {
11102                   /*
11103                   Type destType
11104                   {
11105                      kind = intType;
11106                      refCount = 1;
11107                   };
11108                   e.exp.destType = destType;
11109                   */
11110
11111                   ProcessExpressionType(exp);
11112                }
11113                if(!exp.next)
11114                   curSwitchType = exp.expType;
11115             }
11116          }
11117          ProcessStatement(stmt.switchStmt.stmt);
11118          curSwitchType = oldSwitchType;
11119          break;
11120       }
11121       case whileStmt:
11122       {
11123          if(stmt.whileStmt.exp)
11124          {
11125             Expression exp;
11126
11127             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11128             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11129             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11130             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11131             {
11132                ProcessExpressionType(exp);
11133             }
11134          }
11135          if(stmt.whileStmt.stmt)
11136             ProcessStatement(stmt.whileStmt.stmt);
11137          break;
11138       }
11139       case doWhileStmt:
11140       {
11141          if(stmt.doWhile.exp)
11142          {
11143             Expression exp;
11144
11145             if(stmt.doWhile.exp->last)
11146             {
11147                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11148                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11149                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11150             }
11151             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11152             {
11153                ProcessExpressionType(exp);
11154             }
11155          }
11156          if(stmt.doWhile.stmt)
11157             ProcessStatement(stmt.doWhile.stmt);
11158          break;
11159       }
11160       case forStmt:
11161       {
11162          Expression exp;
11163          if(stmt.forStmt.init)
11164             ProcessStatement(stmt.forStmt.init);
11165
11166          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11167          {
11168             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11169             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11170             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11171          }
11172
11173          if(stmt.forStmt.check)
11174             ProcessStatement(stmt.forStmt.check);
11175          if(stmt.forStmt.increment)
11176          {
11177             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11178                ProcessExpressionType(exp);
11179          }
11180
11181          if(stmt.forStmt.stmt)
11182             ProcessStatement(stmt.forStmt.stmt);
11183          break;
11184       }
11185       case forEachStmt:
11186       {
11187          Identifier id = stmt.forEachStmt.id;
11188          OldList * exp = stmt.forEachStmt.exp;
11189          OldList * filter = stmt.forEachStmt.filter;
11190          Statement block = stmt.forEachStmt.stmt;
11191          char iteratorType[1024];
11192          Type source;
11193          Expression e;
11194          bool isBuiltin = exp && exp->last &&
11195             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11196               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11197          Expression arrayExp;
11198          char * typeString = null;
11199          int builtinCount = 0;
11200
11201          for(e = exp ? exp->first : null; e; e = e.next)
11202          {
11203             if(!e.next)
11204             {
11205                FreeType(e.destType);
11206                e.destType = ProcessTypeString("Container", false);
11207             }
11208             if(!isBuiltin || e.next)
11209                ProcessExpressionType(e);
11210          }
11211
11212          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11213          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11214             eClass_IsDerived(source._class.registered, containerClass)))
11215          {
11216             Class _class = source ? source._class.registered : null;
11217             Symbol symbol;
11218             Expression expIt = null;
11219             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11220             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11221             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11222             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11223             stmt.type = compoundStmt;
11224
11225             stmt.compound.context = Context { };
11226             stmt.compound.context.parent = curContext;
11227             curContext = stmt.compound.context;
11228
11229             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11230             {
11231                Class mapClass = eSystem_FindClass(privateModule, "Map");
11232                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11233                isCustomAVLTree = true;
11234                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11235                   isAVLTree = true;
11236                else if(eClass_IsDerived(source._class.registered, mapClass))
11237                   isMap = true;
11238             }
11239             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11240             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11241             {
11242                Class listClass = eSystem_FindClass(privateModule, "List");
11243                isLinkList = true;
11244                isList = eClass_IsDerived(source._class.registered, listClass);
11245             }
11246
11247             if(isArray)
11248             {
11249                Declarator decl;
11250                OldList * specs = MkList();
11251                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11252                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11253                stmt.compound.declarations = MkListOne(
11254                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11255                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11256                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11257                      MkInitializerAssignment(MkExpBrackets(exp))))));
11258             }
11259             else if(isBuiltin)
11260             {
11261                Type type = null;
11262                char typeStringBuf[1024];
11263
11264                // TODO: Merge this code?
11265                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11266                if(((Expression)exp->last).type == castExp)
11267                {
11268                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11269                   if(typeName)
11270                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11271                }
11272
11273                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11274                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11275                   arrayExp.destType._class.registered.templateArgs)
11276                {
11277                   Class templateClass = arrayExp.destType._class.registered;
11278                   typeString = templateClass.templateArgs[2].dataTypeString;
11279                }
11280                else if(arrayExp.list)
11281                {
11282                   // Guess type from expressions in the array
11283                   Expression e;
11284                   for(e = arrayExp.list->first; e; e = e.next)
11285                   {
11286                      ProcessExpressionType(e);
11287                      if(e.expType)
11288                      {
11289                         if(!type) { type = e.expType; type.refCount++; }
11290                         else
11291                         {
11292                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11293                            if(!MatchTypeExpression(e, type, null, false))
11294                            {
11295                               FreeType(type);
11296                               type = e.expType;
11297                               e.expType = null;
11298
11299                               e = arrayExp.list->first;
11300                               ProcessExpressionType(e);
11301                               if(e.expType)
11302                               {
11303                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11304                                  if(!MatchTypeExpression(e, type, null, false))
11305                                  {
11306                                     FreeType(e.expType);
11307                                     e.expType = null;
11308                                     FreeType(type);
11309                                     type = null;
11310                                     break;
11311                                  }
11312                               }
11313                            }
11314                         }
11315                         if(e.expType)
11316                         {
11317                            FreeType(e.expType);
11318                            e.expType = null;
11319                         }
11320                      }
11321                   }
11322                   if(type)
11323                   {
11324                      typeStringBuf[0] = '\0';
11325                      PrintType(type, typeStringBuf, false, true);
11326                      typeString = typeStringBuf;
11327                      FreeType(type);
11328                   }
11329                }
11330                if(typeString)
11331                {
11332                   OldList * initializers = MkList();
11333                   Declarator decl;
11334                   OldList * specs = MkList();
11335                   if(arrayExp.list)
11336                   {
11337                      Expression e;
11338
11339                      builtinCount = arrayExp.list->count;
11340                      type = ProcessTypeString(typeString, false);
11341                      while(e = arrayExp.list->first)
11342                      {
11343                         arrayExp.list->Remove(e);
11344                         e.destType = type;
11345                         type.refCount++;
11346                         ProcessExpressionType(e);
11347                         ListAdd(initializers, MkInitializerAssignment(e));
11348                      }
11349                      FreeType(type);
11350                      delete arrayExp.list;
11351                   }
11352                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11353                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11354                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11355
11356                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11357                      PlugDeclarator(
11358                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11359                         ), MkInitializerList(initializers)))));
11360                   FreeList(exp, FreeExpression);
11361                }
11362                else
11363                {
11364                   arrayExp.expType = ProcessTypeString("Container", false);
11365                   Compiler_Error($"Couldn't determine type of array elements\n");
11366                }
11367
11368                /*
11369                Declarator decl;
11370                OldList * specs = MkList();
11371
11372                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11373                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11374                stmt.compound.declarations = MkListOne(
11375                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11376                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11377                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11378                      MkInitializerAssignment(MkExpBrackets(exp))))));
11379                */
11380             }
11381             else if(isLinkList && !isList)
11382             {
11383                Declarator decl;
11384                OldList * specs = MkList();
11385                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11386                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11387                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11388                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11389                      MkInitializerAssignment(MkExpBrackets(exp))))));
11390             }
11391             /*else if(isCustomAVLTree)
11392             {
11393                Declarator decl;
11394                OldList * specs = MkList();
11395                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11396                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11397                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11398                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11399                      MkInitializerAssignment(MkExpBrackets(exp))))));
11400             }*/
11401             else if(_class.templateArgs)
11402             {
11403                if(isMap)
11404                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11405                else
11406                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11407
11408                stmt.compound.declarations = MkListOne(
11409                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11410                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11411                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11412             }
11413             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11414
11415             if(block)
11416             {
11417                // Reparent sub-contexts in this statement
11418                switch(block.type)
11419                {
11420                   case compoundStmt:
11421                      if(block.compound.context)
11422                         block.compound.context.parent = stmt.compound.context;
11423                      break;
11424                   case ifStmt:
11425                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11426                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11427                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11428                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11429                      break;
11430                   case switchStmt:
11431                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11432                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11433                      break;
11434                   case whileStmt:
11435                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11436                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11437                      break;
11438                   case doWhileStmt:
11439                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11440                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11441                      break;
11442                   case forStmt:
11443                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11444                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11445                      break;
11446                   case forEachStmt:
11447                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11448                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11449                      break;
11450                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11451                   case labeledStmt:
11452                   case caseStmt
11453                   case expressionStmt:
11454                   case gotoStmt:
11455                   case continueStmt:
11456                   case breakStmt
11457                   case returnStmt:
11458                   case asmStmt:
11459                   case badDeclarationStmt:
11460                   case fireWatchersStmt:
11461                   case stopWatchingStmt:
11462                   case watchStmt:
11463                   */
11464                }
11465             }
11466             if(filter)
11467             {
11468                block = MkIfStmt(filter, block, null);
11469             }
11470             if(isArray)
11471             {
11472                stmt.compound.statements = MkListOne(MkForStmt(
11473                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11474                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11475                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11476                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11477                   block));
11478               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11479               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11480               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11481             }
11482             else if(isBuiltin)
11483             {
11484                char count[128];
11485                //OldList * specs = MkList();
11486                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11487
11488                sprintf(count, "%d", builtinCount);
11489
11490                stmt.compound.statements = MkListOne(MkForStmt(
11491                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11492                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11493                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11494                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11495                   block));
11496
11497                /*
11498                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11499                stmt.compound.statements = MkListOne(MkForStmt(
11500                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11501                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11502                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11503                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11504                   block));
11505               */
11506               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11507               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11508               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11509             }
11510             else if(isLinkList && !isList)
11511             {
11512                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11513                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11514                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11515                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11516                {
11517                   stmt.compound.statements = MkListOne(MkForStmt(
11518                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11519                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11520                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11521                      block));
11522                }
11523                else
11524                {
11525                   OldList * specs = MkList();
11526                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11527                   stmt.compound.statements = MkListOne(MkForStmt(
11528                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11529                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11530                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11531                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11532                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11533                      block));
11534                }
11535                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11536                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11537                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11538             }
11539             /*else if(isCustomAVLTree)
11540             {
11541                stmt.compound.statements = MkListOne(MkForStmt(
11542                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11543                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11544                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11545                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11546                   block));
11547
11548                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11549                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11550                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11551             }*/
11552             else
11553             {
11554                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11555                   MkIdentifier("Next")), null)), block));
11556             }
11557             ProcessExpressionType(expIt);
11558             if(stmt.compound.declarations->first)
11559                ProcessDeclaration(stmt.compound.declarations->first);
11560
11561             if(symbol)
11562                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11563
11564             ProcessStatement(stmt);
11565             curContext = stmt.compound.context.parent;
11566             break;
11567          }
11568          else
11569          {
11570             Compiler_Error($"Expression is not a container\n");
11571          }
11572          break;
11573       }
11574       case gotoStmt:
11575          break;
11576       case continueStmt:
11577          break;
11578       case breakStmt:
11579          break;
11580       case returnStmt:
11581       {
11582          Expression exp;
11583          if(stmt.expressions)
11584          {
11585             for(exp = stmt.expressions->first; exp; exp = exp.next)
11586             {
11587                if(!exp.next)
11588                {
11589                   if(curFunction && !curFunction.type)
11590                      curFunction.type = ProcessType(
11591                         curFunction.specifiers, curFunction.declarator);
11592                   FreeType(exp.destType);
11593                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11594                   if(exp.destType) exp.destType.refCount++;
11595                }
11596                ProcessExpressionType(exp);
11597             }
11598          }
11599          break;
11600       }
11601       case badDeclarationStmt:
11602       {
11603          ProcessDeclaration(stmt.decl);
11604          break;
11605       }
11606       case asmStmt:
11607       {
11608          AsmField field;
11609          if(stmt.asmStmt.inputFields)
11610          {
11611             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11612                if(field.expression)
11613                   ProcessExpressionType(field.expression);
11614          }
11615          if(stmt.asmStmt.outputFields)
11616          {
11617             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11618                if(field.expression)
11619                   ProcessExpressionType(field.expression);
11620          }
11621          if(stmt.asmStmt.clobberedFields)
11622          {
11623             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11624             {
11625                if(field.expression)
11626                   ProcessExpressionType(field.expression);
11627             }
11628          }
11629          break;
11630       }
11631       case watchStmt:
11632       {
11633          PropertyWatch propWatch;
11634          OldList * watches = stmt._watch.watches;
11635          Expression object = stmt._watch.object;
11636          Expression watcher = stmt._watch.watcher;
11637          if(watcher)
11638             ProcessExpressionType(watcher);
11639          if(object)
11640             ProcessExpressionType(object);
11641
11642          if(inCompiler)
11643          {
11644             if(watcher || thisClass)
11645             {
11646                External external = curExternal;
11647                Context context = curContext;
11648
11649                stmt.type = expressionStmt;
11650                stmt.expressions = MkList();
11651
11652                curExternal = external.prev;
11653
11654                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11655                {
11656                   ClassFunction func;
11657                   char watcherName[1024];
11658                   Class watcherClass = watcher ?
11659                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11660                   External createdExternal;
11661
11662                   // Create a declaration above
11663                   External externalDecl = MkExternalDeclaration(null);
11664                   ast->Insert(curExternal.prev, externalDecl);
11665
11666                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11667                   if(propWatch.deleteWatch)
11668                      strcat(watcherName, "_delete");
11669                   else
11670                   {
11671                      Identifier propID;
11672                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11673                      {
11674                         strcat(watcherName, "_");
11675                         strcat(watcherName, propID.string);
11676                      }
11677                   }
11678
11679                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11680                   {
11681                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11682                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11683                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11684                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11685                      ProcessClassFunctionBody(func, propWatch.compound);
11686                      propWatch.compound = null;
11687
11688                      //afterExternal = afterExternal ? afterExternal : curExternal;
11689
11690                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11691                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11692                      // TESTING THIS...
11693                      createdExternal.symbol.idCode = external.symbol.idCode;
11694
11695                      curExternal = createdExternal;
11696                      ProcessFunction(createdExternal.function);
11697
11698
11699                      // Create a declaration above
11700                      {
11701                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
11702                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11703                         externalDecl.declaration = decl;
11704                         if(decl.symbol && !decl.symbol.pointerExternal)
11705                            decl.symbol.pointerExternal = externalDecl;
11706                      }
11707
11708                      if(propWatch.deleteWatch)
11709                      {
11710                         OldList * args = MkList();
11711                         ListAdd(args, CopyExpression(object));
11712                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11713                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11714                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11715                      }
11716                      else
11717                      {
11718                         Class _class = object.expType._class.registered;
11719                         Identifier propID;
11720
11721                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11722                         {
11723                            char propName[1024];
11724                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11725                            if(prop)
11726                            {
11727                               char getName[1024], setName[1024];
11728                               OldList * args = MkList();
11729
11730                               DeclareProperty(prop, setName, getName);
11731
11732                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11733                               strcpy(propName, "__ecereProp_");
11734                               FullClassNameCat(propName, prop._class.fullName, false);
11735                               strcat(propName, "_");
11736                               // strcat(propName, prop.name);
11737                               FullClassNameCat(propName, prop.name, true);
11738
11739                               ListAdd(args, CopyExpression(object));
11740                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11741                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11742                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11743
11744                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11745                            }
11746                            else
11747                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11748                         }
11749                      }
11750                   }
11751                   else
11752                      Compiler_Error($"Invalid watched object\n");
11753                }
11754
11755                curExternal = external;
11756                curContext = context;
11757
11758                if(watcher)
11759                   FreeExpression(watcher);
11760                if(object)
11761                   FreeExpression(object);
11762                FreeList(watches, FreePropertyWatch);
11763             }
11764             else
11765                Compiler_Error($"No observer specified and not inside a _class\n");
11766          }
11767          else
11768          {
11769             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11770             {
11771                ProcessStatement(propWatch.compound);
11772             }
11773
11774          }
11775          break;
11776       }
11777       case fireWatchersStmt:
11778       {
11779          OldList * watches = stmt._watch.watches;
11780          Expression object = stmt._watch.object;
11781          Class _class;
11782          // DEBUGGER BUG: Why doesn't watches evaluate to null??
11783          // printf("%X\n", watches);
11784          // printf("%X\n", stmt._watch.watches);
11785          if(object)
11786             ProcessExpressionType(object);
11787
11788          if(inCompiler)
11789          {
11790             _class = object ?
11791                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
11792
11793             if(_class)
11794             {
11795                Identifier propID;
11796
11797                stmt.type = expressionStmt;
11798                stmt.expressions = MkList();
11799
11800                // Check if we're inside a property set
11801                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11802                {
11803                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
11804                }
11805                else if(!watches)
11806                {
11807                   //Compiler_Error($"No property specified and not inside a property set\n");
11808                }
11809                if(watches)
11810                {
11811                   for(propID = watches->first; propID; propID = propID.next)
11812                   {
11813                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11814                      if(prop)
11815                      {
11816                         CreateFireWatcher(prop, object, stmt);
11817                      }
11818                      else
11819                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
11820                   }
11821                }
11822                else
11823                {
11824                   // Fire all properties!
11825                   Property prop;
11826                   Class base;
11827                   for(base = _class; base; base = base.base)
11828                   {
11829                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
11830                      {
11831                         if(prop.isProperty && prop.isWatchable)
11832                         {
11833                            CreateFireWatcher(prop, object, stmt);
11834                         }
11835                      }
11836                   }
11837                }
11838
11839                if(object)
11840                   FreeExpression(object);
11841                FreeList(watches, FreeIdentifier);
11842             }
11843             else
11844                Compiler_Error($"Invalid object specified and not inside a class\n");
11845          }
11846          break;
11847       }
11848       case stopWatchingStmt:
11849       {
11850          OldList * watches = stmt._watch.watches;
11851          Expression object = stmt._watch.object;
11852          Expression watcher = stmt._watch.watcher;
11853          Class _class;
11854          if(object)
11855             ProcessExpressionType(object);
11856          if(watcher)
11857             ProcessExpressionType(watcher);
11858          if(inCompiler)
11859          {
11860             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
11861
11862             if(watcher || thisClass)
11863             {
11864                if(_class)
11865                {
11866                   Identifier propID;
11867
11868                   stmt.type = expressionStmt;
11869                   stmt.expressions = MkList();
11870
11871                   if(!watches)
11872                   {
11873                      OldList * args;
11874                      // eInstance_StopWatching(object, null, watcher);
11875                      args = MkList();
11876                      ListAdd(args, CopyExpression(object));
11877                      ListAdd(args, MkExpConstant("0"));
11878                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11879                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11880                   }
11881                   else
11882                   {
11883                      for(propID = watches->first; propID; propID = propID.next)
11884                      {
11885                         char propName[1024];
11886                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11887                         if(prop)
11888                         {
11889                            char getName[1024], setName[1024];
11890                            OldList * args = MkList();
11891
11892                            DeclareProperty(prop, setName, getName);
11893
11894                            // eInstance_StopWatching(object, prop, watcher);
11895                            strcpy(propName, "__ecereProp_");
11896                            FullClassNameCat(propName, prop._class.fullName, false);
11897                            strcat(propName, "_");
11898                            // strcat(propName, prop.name);
11899                            FullClassNameCat(propName, prop.name, true);
11900                            MangleClassName(propName);
11901
11902                            ListAdd(args, CopyExpression(object));
11903                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11904                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11905                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11906                         }
11907                         else
11908                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11909                      }
11910                   }
11911
11912                   if(object)
11913                      FreeExpression(object);
11914                   if(watcher)
11915                      FreeExpression(watcher);
11916                   FreeList(watches, FreeIdentifier);
11917                }
11918                else
11919                   Compiler_Error($"Invalid object specified and not inside a class\n");
11920             }
11921             else
11922                Compiler_Error($"No observer specified and not inside a class\n");
11923          }
11924          break;
11925       }
11926    }
11927 }
11928
11929 static void ProcessFunction(FunctionDefinition function)
11930 {
11931    Identifier id = GetDeclId(function.declarator);
11932    Symbol symbol = function.declarator ? function.declarator.symbol : null;
11933    Type type = symbol ? symbol.type : null;
11934    Class oldThisClass = thisClass;
11935    Context oldTopContext = topContext;
11936
11937    yylloc = function.loc;
11938    // Process thisClass
11939
11940    if(type && type.thisClass)
11941    {
11942       Symbol classSym = type.thisClass;
11943       Class _class = type.thisClass.registered;
11944       char className[1024];
11945       char structName[1024];
11946       Declarator funcDecl;
11947       Symbol thisSymbol;
11948
11949       bool typedObject = false;
11950
11951       if(_class && !_class.base)
11952       {
11953          _class = currentClass;
11954          if(_class && !_class.symbol)
11955             _class.symbol = FindClass(_class.fullName);
11956          classSym = _class ? _class.symbol : null;
11957          typedObject = true;
11958       }
11959
11960       thisClass = _class;
11961
11962       if(inCompiler && _class)
11963       {
11964          if(type.kind == functionType)
11965          {
11966             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
11967             {
11968                //TypeName param = symbol.type.params.first;
11969                Type param = symbol.type.params.first;
11970                symbol.type.params.Remove(param);
11971                //FreeTypeName(param);
11972                FreeType(param);
11973             }
11974             if(type.classObjectType != classPointer)
11975             {
11976                symbol.type.params.Insert(null, MkClassType(_class.fullName));
11977                symbol.type.staticMethod = true;
11978                symbol.type.thisClass = null;
11979
11980                // HIGH DANGER: VERIFYING THIS...
11981                symbol.type.extraParam = false;
11982             }
11983          }
11984
11985          strcpy(className, "__ecereClass_");
11986          FullClassNameCat(className, _class.fullName, true);
11987
11988          MangleClassName(className);
11989
11990          structName[0] = 0;
11991          FullClassNameCat(structName, _class.fullName, false);
11992
11993          // [class] this
11994
11995
11996          funcDecl = GetFuncDecl(function.declarator);
11997          if(funcDecl)
11998          {
11999             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12000             {
12001                TypeName param = funcDecl.function.parameters->first;
12002                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12003                {
12004                   funcDecl.function.parameters->Remove(param);
12005                   FreeTypeName(param);
12006                }
12007             }
12008
12009             // DANGER: Watch for this... Check if it's a Conversion?
12010             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12011
12012             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12013             if(!function.propertyNoThis)
12014             {
12015                TypeName thisParam;
12016
12017                if(type.classObjectType != classPointer)
12018                {
12019                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12020                   if(!funcDecl.function.parameters)
12021                      funcDecl.function.parameters = MkList();
12022                   funcDecl.function.parameters->Insert(null, thisParam);
12023                }
12024
12025                if(typedObject)
12026                {
12027                   if(type.classObjectType != classPointer)
12028                   {
12029                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12030                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12031                   }
12032
12033                   thisParam = TypeName
12034                   {
12035                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12036                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12037                   };
12038                   funcDecl.function.parameters->Insert(null, thisParam);
12039                }
12040             }
12041          }
12042
12043          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12044          {
12045             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12046             funcDecl = GetFuncDecl(initDecl.declarator);
12047             if(funcDecl)
12048             {
12049                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12050                {
12051                   TypeName param = funcDecl.function.parameters->first;
12052                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12053                   {
12054                      funcDecl.function.parameters->Remove(param);
12055                      FreeTypeName(param);
12056                   }
12057                }
12058
12059                if(type.classObjectType != classPointer)
12060                {
12061                   // DANGER: Watch for this... Check if it's a Conversion?
12062                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12063                   {
12064                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12065
12066                      if(!funcDecl.function.parameters)
12067                         funcDecl.function.parameters = MkList();
12068                      funcDecl.function.parameters->Insert(null, thisParam);
12069                   }
12070                }
12071             }
12072          }
12073       }
12074
12075       // Add this to the context
12076       if(function.body)
12077       {
12078          if(type.classObjectType != classPointer)
12079          {
12080             thisSymbol = Symbol
12081             {
12082                string = CopyString("this");
12083                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12084             };
12085             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12086
12087             if(typedObject && thisSymbol.type)
12088             {
12089                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12090                thisSymbol.type.byReference = type.byReference;
12091                thisSymbol.type.typedByReference = type.byReference;
12092                /*
12093                thisSymbol = Symbol { string = CopyString("class") };
12094                function.body.compound.context.symbols.Add(thisSymbol);
12095                */
12096             }
12097          }
12098       }
12099
12100       // Pointer to class data
12101
12102       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12103       {
12104          DataMember member = null;
12105          {
12106             Class base;
12107             for(base = _class; base && base.type != systemClass; base = base.next)
12108             {
12109                for(member = base.membersAndProperties.first; member; member = member.next)
12110                   if(!member.isProperty)
12111                      break;
12112                if(member)
12113                   break;
12114             }
12115          }
12116          for(member = _class.membersAndProperties.first; member; member = member.next)
12117             if(!member.isProperty)
12118                break;
12119          if(member)
12120          {
12121             char pointerName[1024];
12122
12123             Declaration decl;
12124             Initializer initializer;
12125             Expression exp, bytePtr;
12126
12127             strcpy(pointerName, "__ecerePointer_");
12128             FullClassNameCat(pointerName, _class.fullName, false);
12129             {
12130                char className[1024];
12131                strcpy(className, "__ecereClass_");
12132                FullClassNameCat(className, classSym.string, true);
12133                MangleClassName(className);
12134
12135                // Testing This
12136                DeclareClass(classSym, className);
12137             }
12138
12139             // ((byte *) this)
12140             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12141
12142             if(_class.fixed)
12143             {
12144                char string[256];
12145                sprintf(string, "%d", _class.offset);
12146                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12147             }
12148             else
12149             {
12150                // ([bytePtr] + [className]->offset)
12151                exp = QBrackets(MkExpOp(bytePtr, '+',
12152                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12153             }
12154
12155             // (this ? [exp] : 0)
12156             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12157             exp.expType = Type
12158             {
12159                refCount = 1;
12160                kind = pointerType;
12161                type = Type { refCount = 1, kind = voidType };
12162             };
12163
12164             if(function.body)
12165             {
12166                yylloc = function.body.loc;
12167                // ([structName] *) [exp]
12168                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12169                initializer = MkInitializerAssignment(
12170                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12171
12172                // [structName] * [pointerName] = [initializer];
12173                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12174
12175                {
12176                   Context prevContext = curContext;
12177                   curContext = function.body.compound.context;
12178
12179                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12180                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12181
12182                   curContext = prevContext;
12183                }
12184
12185                // WHY?
12186                decl.symbol = null;
12187
12188                if(!function.body.compound.declarations)
12189                   function.body.compound.declarations = MkList();
12190                function.body.compound.declarations->Insert(null, decl);
12191             }
12192          }
12193       }
12194
12195
12196       // Loop through the function and replace undeclared identifiers
12197       // which are a member of the class (methods, properties or data)
12198       // by "this.[member]"
12199    }
12200    else
12201       thisClass = null;
12202
12203    if(id)
12204    {
12205       FreeSpecifier(id._class);
12206       id._class = null;
12207
12208       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12209       {
12210          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12211          id = GetDeclId(initDecl.declarator);
12212
12213          FreeSpecifier(id._class);
12214          id._class = null;
12215       }
12216    }
12217    if(function.body)
12218       topContext = function.body.compound.context;
12219    {
12220       FunctionDefinition oldFunction = curFunction;
12221       curFunction = function;
12222       if(function.body)
12223          ProcessStatement(function.body);
12224
12225       // If this is a property set and no firewatchers has been done yet, add one here
12226       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12227       {
12228          Statement prevCompound = curCompound;
12229          Context prevContext = curContext;
12230
12231          Statement fireWatchers = MkFireWatchersStmt(null, null);
12232          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12233          ListAdd(function.body.compound.statements, fireWatchers);
12234
12235          curCompound = function.body;
12236          curContext = function.body.compound.context;
12237
12238          ProcessStatement(fireWatchers);
12239
12240          curContext = prevContext;
12241          curCompound = prevCompound;
12242
12243       }
12244
12245       curFunction = oldFunction;
12246    }
12247
12248    if(function.declarator)
12249    {
12250       ProcessDeclarator(function.declarator);
12251    }
12252
12253    topContext = oldTopContext;
12254    thisClass = oldThisClass;
12255 }
12256
12257 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12258 static void ProcessClass(OldList definitions, Symbol symbol)
12259 {
12260    ClassDef def;
12261    External external = curExternal;
12262    Class regClass = symbol ? symbol.registered : null;
12263
12264    // Process all functions
12265    for(def = definitions.first; def; def = def.next)
12266    {
12267       if(def.type == functionClassDef)
12268       {
12269          if(def.function.declarator)
12270             curExternal = def.function.declarator.symbol.pointerExternal;
12271          else
12272             curExternal = external;
12273
12274          ProcessFunction((FunctionDefinition)def.function);
12275       }
12276       else if(def.type == declarationClassDef)
12277       {
12278          if(def.decl.type == instDeclaration)
12279          {
12280             thisClass = regClass;
12281             ProcessInstantiationType(def.decl.inst);
12282             thisClass = null;
12283          }
12284          // Testing this
12285          else
12286          {
12287             Class backThisClass = thisClass;
12288             if(regClass) thisClass = regClass;
12289             ProcessDeclaration(def.decl);
12290             thisClass = backThisClass;
12291          }
12292       }
12293       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12294       {
12295          MemberInit defProperty;
12296
12297          // Add this to the context
12298          Symbol thisSymbol = Symbol
12299          {
12300             string = CopyString("this");
12301             type = regClass ? MkClassType(regClass.fullName) : null;
12302          };
12303          globalContext.symbols.Add((BTNode)thisSymbol);
12304
12305          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12306          {
12307             thisClass = regClass;
12308             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12309             thisClass = null;
12310          }
12311
12312          globalContext.symbols.Remove((BTNode)thisSymbol);
12313          FreeSymbol(thisSymbol);
12314       }
12315       else if(def.type == propertyClassDef && def.propertyDef)
12316       {
12317          PropertyDef prop = def.propertyDef;
12318
12319          // Add this to the context
12320          /*
12321          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12322          globalContext.symbols.Add(thisSymbol);
12323          */
12324
12325          thisClass = regClass;
12326          if(prop.setStmt)
12327          {
12328             if(regClass)
12329             {
12330                Symbol thisSymbol
12331                {
12332                   string = CopyString("this");
12333                   type = MkClassType(regClass.fullName);
12334                };
12335                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12336             }
12337
12338             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12339             ProcessStatement(prop.setStmt);
12340          }
12341          if(prop.getStmt)
12342          {
12343             if(regClass)
12344             {
12345                Symbol thisSymbol
12346                {
12347                   string = CopyString("this");
12348                   type = MkClassType(regClass.fullName);
12349                };
12350                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12351             }
12352
12353             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12354             ProcessStatement(prop.getStmt);
12355          }
12356          if(prop.issetStmt)
12357          {
12358             if(regClass)
12359             {
12360                Symbol thisSymbol
12361                {
12362                   string = CopyString("this");
12363                   type = MkClassType(regClass.fullName);
12364                };
12365                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12366             }
12367
12368             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12369             ProcessStatement(prop.issetStmt);
12370          }
12371
12372          thisClass = null;
12373
12374          /*
12375          globalContext.symbols.Remove(thisSymbol);
12376          FreeSymbol(thisSymbol);
12377          */
12378       }
12379       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12380       {
12381          PropertyWatch propertyWatch = def.propertyWatch;
12382
12383          thisClass = regClass;
12384          if(propertyWatch.compound)
12385          {
12386             Symbol thisSymbol
12387             {
12388                string = CopyString("this");
12389                type = regClass ? MkClassType(regClass.fullName) : null;
12390             };
12391
12392             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12393
12394             curExternal = null;
12395             ProcessStatement(propertyWatch.compound);
12396          }
12397          thisClass = null;
12398       }
12399    }
12400 }
12401
12402 void DeclareFunctionUtil(String s)
12403 {
12404    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12405    if(function)
12406    {
12407       char name[1024];
12408       name[0] = 0;
12409       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12410          strcpy(name, "__ecereFunction_");
12411       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12412       DeclareFunction(function, name);
12413    }
12414 }
12415
12416 void ComputeDataTypes()
12417 {
12418    External external;
12419    External temp { };
12420    External after = null;
12421
12422    currentClass = null;
12423
12424    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12425
12426    for(external = ast->first; external; external = external.next)
12427    {
12428       if(external.type == declarationExternal)
12429       {
12430          Declaration decl = external.declaration;
12431          if(decl)
12432          {
12433             OldList * decls = decl.declarators;
12434             if(decls)
12435             {
12436                InitDeclarator initDecl = decls->first;
12437                if(initDecl)
12438                {
12439                   Declarator declarator = initDecl.declarator;
12440                   if(declarator && declarator.type == identifierDeclarator)
12441                   {
12442                      Identifier id = declarator.identifier;
12443                      if(id && id.string)
12444                      {
12445                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12446                         {
12447                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12448                            after = external;
12449                         }
12450                      }
12451                   }
12452                }
12453             }
12454          }
12455        }
12456    }
12457
12458    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12459    ast->Insert(after, temp);
12460    curExternal = temp;
12461
12462    DeclareFunctionUtil("eSystem_New");
12463    DeclareFunctionUtil("eSystem_New0");
12464    DeclareFunctionUtil("eSystem_Renew");
12465    DeclareFunctionUtil("eSystem_Renew0");
12466    DeclareFunctionUtil("eSystem_Delete");
12467    DeclareFunctionUtil("eClass_GetProperty");
12468    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12469
12470    DeclareStruct("ecere::com::Class", false);
12471    DeclareStruct("ecere::com::Instance", false);
12472    DeclareStruct("ecere::com::Property", false);
12473    DeclareStruct("ecere::com::DataMember", false);
12474    DeclareStruct("ecere::com::Method", false);
12475    DeclareStruct("ecere::com::SerialBuffer", false);
12476    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12477
12478    ast->Remove(temp);
12479
12480    for(external = ast->first; external; external = external.next)
12481    {
12482       afterExternal = curExternal = external;
12483       if(external.type == functionExternal)
12484       {
12485          currentClass = external.function._class;
12486          ProcessFunction(external.function);
12487       }
12488       // There shouldn't be any _class member access here anyways...
12489       else if(external.type == declarationExternal)
12490       {
12491          currentClass = null;
12492          ProcessDeclaration(external.declaration);
12493       }
12494       else if(external.type == classExternal)
12495       {
12496          ClassDefinition _class = external._class;
12497          currentClass = external.symbol.registered;
12498          if(_class.definitions)
12499          {
12500             ProcessClass(_class.definitions, _class.symbol);
12501          }
12502          if(inCompiler)
12503          {
12504             // Free class data...
12505             ast->Remove(external);
12506             delete external;
12507          }
12508       }
12509       else if(external.type == nameSpaceExternal)
12510       {
12511          thisNameSpace = external.id.string;
12512       }
12513    }
12514    currentClass = null;
12515    thisNameSpace = null;
12516
12517    delete temp.symbol;
12518    delete temp;
12519 }