compiler/libec: Fixed null pointer compiler crashes
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 // UNTIL IMPLEMENTED IN GRAMMAR
4 #define ACCESS_CLASSDATA(_class, baseClass) \
5    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
6
7 #define YYLTYPE Location
8 #include "grammar.h"
9
10 extern OldList * ast;
11 extern int returnCode;
12 extern Expression parsedExpression;
13 extern bool yydebug;
14 public void SetYydebug(bool b) { yydebug = b; }
15 extern bool echoOn;
16
17 void resetScanner();
18
19 // TODO: Reset this to 0 on reinitialization
20 int propWatcherID;
21
22 int expression_yyparse();
23 static Statement curCompound;
24 External curExternal, afterExternal;
25 static Type curSwitchType;
26 static Class currentClass;
27 Class thisClass;
28 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
29 static char * thisNameSpace;
30 /*static */Class containerClass;
31 bool thisClassParams = true;
32
33 uint internalValueCounter;
34
35 #ifdef _DEBUG
36 Time findSymbolTotalTime;
37 #endif
38
39 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
40 /*static */public void PrintExpression(Expression exp, char * string)
41 {
42    //if(inCompiler)
43    {
44       TempFile f { };
45       int count;
46       bool backOutputLineNumbers = outputLineNumbers;
47       outputLineNumbers = false;
48
49       if(exp)
50          OutputExpression(exp, f);
51       f.Seek(0, start);
52       count = strlen(string);
53       count += f.Read(string + count, 1, 1023);
54       string[count] = '\0';
55       delete f;
56
57       outputLineNumbers = backOutputLineNumbers;
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 && type1.isLong == type2.isLong)
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 (type1.type && type2.type && type1.type.constant != type2.type.constant) || 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             if(!ctx) break;   // This happened opening old mapTileCache.ec from archives?
125             symbol = (Symbol)ctx.symbols.FindString(id.string);
126             if(symbol) break;
127          }
128       }
129
130       // If it is not, check if it is a member of the _class
131       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
132       {
133          Property prop = eClass_FindProperty(_class, id.string, privateModule);
134          Method method = null;
135          DataMember member = null;
136          ClassProperty classProp = null;
137          if(!prop)
138          {
139             method = eClass_FindMethod(_class, id.string, privateModule);
140          }
141          if(!prop && !method)
142             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
143          if(!prop && !method && !member)
144          {
145             classProp = eClass_FindClassProperty(_class, id.string);
146          }
147          if(prop || method || member || classProp)
148          {
149             // Replace by this.[member]
150             exp.type = memberExp;
151             exp.member.member = id;
152             exp.member.memberType = unresolvedMember;
153             exp.member.exp = QMkExpId("this");
154             //exp.member.exp.loc = exp.loc;
155             exp.addedThis = true;
156          }
157          else if(_class && _class.templateParams.first)
158          {
159             Class sClass;
160             for(sClass = _class; sClass; sClass = sClass.base)
161             {
162                if(sClass.templateParams.first)
163                {
164                   ClassTemplateParameter param;
165                   for(param = sClass.templateParams.first; param; param = param.next)
166                   {
167                      if(param.type == expression && !strcmp(param.name, id.string))
168                      {
169                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
170
171                         if(argExp)
172                         {
173                            Declarator decl;
174                            OldList * specs = MkList();
175
176                            FreeIdentifier(exp.member.member);
177
178                            ProcessExpressionType(argExp);
179
180                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
181
182                            exp.expType = ProcessType(specs, decl);
183
184                            // *[expType] *[argExp]
185                            exp.type = bracketsExp;
186                            exp.list = MkListOne(MkExpOp(null, '*',
187                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
188                         }
189                      }
190                   }
191                }
192             }
193          }
194       }
195    }
196 }
197
198 ////////////////////////////////////////////////////////////////////////
199 // PRINTING ////////////////////////////////////////////////////////////
200 ////////////////////////////////////////////////////////////////////////
201
202 public char * PrintInt(int64 result)
203 {
204    char temp[100];
205    if(result > MAXINT)
206       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
207    else
208       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
209    if(result > MAXINT || result < MININT)
210       strcat(temp, "LL");
211    return CopyString(temp);
212 }
213
214 public char * PrintUInt(uint64 result)
215 {
216    char temp[100];
217    if(result > MAXDWORD)
218       sprintf(temp, FORMAT64HEXLL /*"0x%I64X"*/, result);
219    else if(result > MAXINT)
220       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
221    else
222       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
223    return CopyString(temp);
224 }
225
226 public char *  PrintInt64(int64 result)
227 {
228    char temp[100];
229    if(result > MAXINT || result < MININT)
230       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
231    else
232       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
233    return CopyString(temp);
234 }
235
236 public char * PrintUInt64(uint64 result)
237 {
238    char temp[100];
239    if(result > MAXDWORD)
240       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
241    else if(result > MAXINT)
242       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
243    else
244       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
245    return CopyString(temp);
246 }
247
248 public char * PrintHexUInt(uint64 result)
249 {
250    char temp[100];
251    if(result > MAXDWORD)
252       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
253    else
254       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
255    if(result > MAXDWORD)
256       strcat(temp, "LL");
257    return CopyString(temp);
258 }
259
260 public char * PrintHexUInt64(uint64 result)
261 {
262    char temp[100];
263    if(result > MAXDWORD)
264       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
265    else
266       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
267    return CopyString(temp);
268 }
269
270 public char * PrintShort(short result)
271 {
272    char temp[100];
273    sprintf(temp, "%d", (unsigned short)result);
274    return CopyString(temp);
275 }
276
277 public char * PrintUShort(unsigned short result)
278 {
279    char temp[100];
280    if(result > 32767)
281       sprintf(temp, "0x%X", (int)result);
282    else
283       sprintf(temp, "%d", (int)result);
284    return CopyString(temp);
285 }
286
287 public char * PrintChar(char result)
288 {
289    char temp[100];
290    if(result > 0 && isprint(result))
291       sprintf(temp, "'%c'", result);
292    else if(result < 0)
293       sprintf(temp, "%d", (int)result);
294    else
295       //sprintf(temp, "%#X", result);
296       sprintf(temp, "0x%X", (unsigned char)result);
297    return CopyString(temp);
298 }
299
300 public char * PrintUChar(unsigned char result)
301 {
302    char temp[100];
303    sprintf(temp, "0x%X", result);
304    return CopyString(temp);
305 }
306
307 public char * PrintFloat(float result)
308 {
309    char temp[350];
310    if(result.isInf)
311    {
312       if(result.signBit)
313          strcpy(temp, "-inf");
314       else
315          strcpy(temp, "inf");
316    }
317    else if(result.isNan)
318    {
319       if(result.signBit)
320          strcpy(temp, "-nan");
321       else
322          strcpy(temp, "nan");
323    }
324    else
325       sprintf(temp, "%.16ff", result);
326    return CopyString(temp);
327 }
328
329 public char * PrintDouble(double result)
330 {
331    char temp[350];
332    if(result.isInf)
333    {
334       if(result.signBit)
335          strcpy(temp, "-inf");
336       else
337          strcpy(temp, "inf");
338    }
339    else if(result.isNan)
340    {
341       if(result.signBit)
342          strcpy(temp, "-nan");
343       else
344          strcpy(temp, "nan");
345    }
346    else
347       sprintf(temp, "%.16f", result);
348    return CopyString(temp);
349 }
350
351 ////////////////////////////////////////////////////////////////////////
352 ////////////////////////////////////////////////////////////////////////
353
354 //public Operand GetOperand(Expression exp);
355
356 #define GETVALUE(name, t) \
357    public bool GetOp##name(Operand op2, t * value2) \
358    {                                                        \
359       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
360       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
361       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
362       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
363       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
364       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
365       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
366       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
367       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
368       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
369       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
370       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
371       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
372       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
373       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
374       else                                                                          \
375          return false;                                                              \
376       return true;                                                                  \
377    } \
378    public bool Get##name(Expression exp, t * value2) \
379    {                                                        \
380       Operand op2 = GetOperand(exp);                        \
381       return GetOp##name(op2, value2); \
382    }
383
384 // To help the debugger currently not preprocessing...
385 #define HELP(x) x
386
387 GETVALUE(Int, HELP(int));
388 GETVALUE(UInt, HELP(unsigned int));
389 GETVALUE(Int64, HELP(int64));
390 GETVALUE(UInt64, HELP(uint64));
391 GETVALUE(IntPtr, HELP(intptr));
392 GETVALUE(UIntPtr, HELP(uintptr));
393 GETVALUE(IntSize, HELP(intsize));
394 GETVALUE(UIntSize, HELP(uintsize));
395 GETVALUE(Short, HELP(short));
396 GETVALUE(UShort, HELP(unsigned short));
397 GETVALUE(Char, HELP(char));
398 GETVALUE(UChar, HELP(unsigned char));
399 GETVALUE(Float, HELP(float));
400 GETVALUE(Double, HELP(double));
401
402 void ComputeExpression(Expression exp);
403
404 void ComputeClassMembers(Class _class, bool isMember)
405 {
406    DataMember member = isMember ? (DataMember) _class : null;
407    Context context = isMember ? null : SetupTemplatesContext(_class);
408    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
409                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
410    {
411       int unionMemberOffset = 0;
412       int bitFields = 0;
413
414       /*
415       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
416          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
417       */
418
419       if(member)
420       {
421          member.memberOffset = 0;
422          if(targetBits < sizeof(void *) * 8)
423             member.structAlignment = 0;
424       }
425       else if(targetBits < sizeof(void *) * 8)
426          _class.structAlignment = 0;
427
428       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
429       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
430          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
431
432       if(!member && _class.destructionWatchOffset)
433          _class.memberOffset += sizeof(OldList);
434
435       // To avoid reentrancy...
436       //_class.structSize = -1;
437
438       {
439          DataMember dataMember;
440          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
441          {
442             if(!dataMember.isProperty)
443             {
444                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
445                {
446                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
447                   /*if(!dataMember.dataType)
448                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
449                      */
450                }
451             }
452          }
453       }
454
455       {
456          DataMember dataMember;
457          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
458          {
459             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
460             {
461                if(!isMember && _class.type == bitClass && dataMember.dataType)
462                {
463                   BitMember bitMember = (BitMember) dataMember;
464                   uint64 mask = 0;
465                   int d;
466
467                   ComputeTypeSize(dataMember.dataType);
468
469                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
470                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
471
472                   _class.memberOffset = bitMember.pos + bitMember.size;
473                   for(d = 0; d<bitMember.size; d++)
474                   {
475                      if(d)
476                         mask <<= 1;
477                      mask |= 1;
478                   }
479                   bitMember.mask = mask << bitMember.pos;
480                }
481                else if(dataMember.type == normalMember && dataMember.dataType)
482                {
483                   int size;
484                   int alignment = 0;
485
486                   // Prevent infinite recursion
487                   if(dataMember.dataType.kind != classType ||
488                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
489                      _class.type != structClass)))
490                      ComputeTypeSize(dataMember.dataType);
491
492                   if(dataMember.dataType.bitFieldCount)
493                   {
494                      bitFields += dataMember.dataType.bitFieldCount;
495                      size = 0;
496                   }
497                   else
498                   {
499                      if(bitFields)
500                      {
501                         int size = (bitFields + 7) / 8;
502
503                         if(isMember)
504                         {
505                            // TESTING THIS PADDING CODE
506                            if(alignment)
507                            {
508                               member.structAlignment = Max(member.structAlignment, alignment);
509
510                               if(member.memberOffset % alignment)
511                                  member.memberOffset += alignment - (member.memberOffset % alignment);
512                            }
513
514                            dataMember.offset = member.memberOffset;
515                            if(member.type == unionMember)
516                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
517                            else
518                            {
519                               member.memberOffset += size;
520                            }
521                         }
522                         else
523                         {
524                            // TESTING THIS PADDING CODE
525                            if(alignment)
526                            {
527                               _class.structAlignment = Max(_class.structAlignment, alignment);
528
529                               if(_class.memberOffset % alignment)
530                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
531                            }
532
533                            dataMember.offset = _class.memberOffset;
534                            _class.memberOffset += size;
535                         }
536                         bitFields = 0;
537                      }
538                      size = dataMember.dataType.size;
539                      alignment = dataMember.dataType.alignment;
540                   }
541
542                   if(isMember)
543                   {
544                      // TESTING THIS PADDING CODE
545                      if(alignment)
546                      {
547                         member.structAlignment = Max(member.structAlignment, alignment);
548
549                         if(member.memberOffset % alignment)
550                            member.memberOffset += alignment - (member.memberOffset % alignment);
551                      }
552
553                      dataMember.offset = member.memberOffset;
554                      if(member.type == unionMember)
555                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
556                      else
557                      {
558                         member.memberOffset += size;
559                      }
560                   }
561                   else
562                   {
563                      // TESTING THIS PADDING CODE
564                      if(alignment)
565                      {
566                         _class.structAlignment = Max(_class.structAlignment, alignment);
567
568                         if(_class.memberOffset % alignment)
569                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
570                      }
571
572                      dataMember.offset = _class.memberOffset;
573                      _class.memberOffset += size;
574                   }
575                }
576                else
577                {
578                   int alignment;
579
580                   ComputeClassMembers((Class)dataMember, true);
581                   alignment = dataMember.structAlignment;
582
583                   if(isMember)
584                   {
585                      if(alignment)
586                      {
587                         if(member.memberOffset % alignment)
588                            member.memberOffset += alignment - (member.memberOffset % alignment);
589
590                         member.structAlignment = Max(member.structAlignment, alignment);
591                      }
592                      dataMember.offset = member.memberOffset;
593                      if(member.type == unionMember)
594                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
595                      else
596                         member.memberOffset += dataMember.memberOffset;
597                   }
598                   else
599                   {
600                      if(alignment)
601                      {
602                         if(_class.memberOffset % alignment)
603                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
604                         _class.structAlignment = Max(_class.structAlignment, alignment);
605                      }
606                      dataMember.offset = _class.memberOffset;
607                      _class.memberOffset += dataMember.memberOffset;
608                   }
609                }
610             }
611          }
612          if(bitFields)
613          {
614             int alignment = 0;
615             int size = (bitFields + 7) / 8;
616
617             if(isMember)
618             {
619                // TESTING THIS PADDING CODE
620                if(alignment)
621                {
622                   member.structAlignment = Max(member.structAlignment, alignment);
623
624                   if(member.memberOffset % alignment)
625                      member.memberOffset += alignment - (member.memberOffset % alignment);
626                }
627
628                if(member.type == unionMember)
629                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
630                else
631                {
632                   member.memberOffset += size;
633                }
634             }
635             else
636             {
637                // TESTING THIS PADDING CODE
638                if(alignment)
639                {
640                   _class.structAlignment = Max(_class.structAlignment, alignment);
641
642                   if(_class.memberOffset % alignment)
643                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
644                }
645                _class.memberOffset += size;
646             }
647             bitFields = 0;
648          }
649       }
650       if(member && member.type == unionMember)
651       {
652          member.memberOffset = unionMemberOffset;
653       }
654
655       if(!isMember)
656       {
657          /*if(_class.type == structClass)
658             _class.size = _class.memberOffset;
659          else
660          */
661
662          if(_class.type != bitClass)
663          {
664             int extra = 0;
665             if(_class.structAlignment)
666             {
667                if(_class.memberOffset % _class.structAlignment)
668                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
669             }
670             _class.structSize = (_class.base ? (_class.base.templateClass ?
671                (_class.base.type == noHeadClass ? _class.base.templateClass.memberOffset : _class.base.templateClass.structSize) :
672                   (_class.base.type == noHeadClass ? _class.base.memberOffset : _class.base.structSize) ) : 0) + _class.memberOffset + extra;
673             if(!member)
674             {
675                Property prop;
676                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
677                {
678                   if(prop.isProperty && prop.isWatchable)
679                   {
680                      prop.watcherOffset = _class.structSize;
681                      _class.structSize += sizeof(OldList);
682                   }
683                }
684             }
685
686             // Fix Derivatives
687             {
688                OldLink derivative;
689                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
690                {
691                   Class deriv = derivative.data;
692
693                   if(deriv.computeSize)
694                   {
695                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
696                      deriv.offset = /*_class.offset + */(_class.type == noHeadClass ? _class.memberOffset : _class.structSize);
697                      deriv.memberOffset = 0;
698                      // ----------------------
699
700                      deriv.structSize = deriv.offset;
701
702                      ComputeClassMembers(deriv, false);
703                   }
704                }
705             }
706          }
707       }
708    }
709    if(context)
710       FinishTemplatesContext(context);
711 }
712
713 public void ComputeModuleClasses(Module module)
714 {
715    Class _class;
716    OldLink subModule;
717
718    for(subModule = module.modules.first; subModule; subModule = subModule.next)
719       ComputeModuleClasses(subModule.data);
720    for(_class = module.classes.first; _class; _class = _class.next)
721       ComputeClassMembers(_class, false);
722 }
723
724
725 public int ComputeTypeSize(Type type)
726 {
727    uint size = type ? type.size : 0;
728    if(!size && type && !type.computing)
729    {
730       type.computing = true;
731       switch(type.kind)
732       {
733          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
734          case charType: type.alignment = size = sizeof(char); break;
735          case intType: type.alignment = size = sizeof(int); break;
736          case int64Type: type.alignment = size = sizeof(int64); break;
737          case intPtrType: type.alignment = size = targetBits / 8; type.pointerAlignment = true; break;
738          case intSizeType: type.alignment = size = targetBits / 8; type.pointerAlignment = true; break;
739          case longType: type.alignment = size = sizeof(long); break;
740          case shortType: type.alignment = size = sizeof(short); break;
741          case floatType: type.alignment = size = sizeof(float); break;
742          case doubleType: type.alignment = size = sizeof(double); break;
743          case classType:
744          {
745             Class _class = type._class ? type._class.registered : null;
746
747             if(_class && _class.type == structClass)
748             {
749                // Ensure all members are properly registered
750                ComputeClassMembers(_class, false);
751                type.alignment = _class.structAlignment;
752                type.pointerAlignment = (bool)_class.pointerAlignment;
753                size = _class.structSize;
754                if(type.alignment && size % type.alignment)
755                   size += type.alignment - (size % type.alignment);
756
757             }
758             else if(_class && (_class.type == unitClass ||
759                    _class.type == enumClass ||
760                    _class.type == bitClass))
761             {
762                if(!_class.dataType)
763                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
764                size = type.alignment = ComputeTypeSize(_class.dataType);
765             }
766             else
767             {
768                size = type.alignment = targetBits / 8; // sizeof(Instance *);
769                type.pointerAlignment = true;
770             }
771             break;
772          }
773          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */ type.pointerAlignment = true; break;
774          case arrayType:
775             if(type.arraySizeExp)
776             {
777                ProcessExpressionType(type.arraySizeExp);
778                ComputeExpression(type.arraySizeExp);
779                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType &&
780                   type.arraySizeExp.expType.kind != shortType &&
781                   type.arraySizeExp.expType.kind != charType &&
782                   type.arraySizeExp.expType.kind != longType &&
783                   type.arraySizeExp.expType.kind != int64Type &&
784                   type.arraySizeExp.expType.kind != intSizeType &&
785                   type.arraySizeExp.expType.kind != intPtrType &&
786                   type.arraySizeExp.expType.kind != enumType &&
787                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
788                {
789                   Location oldLoc = yylloc;
790                   // bool isConstant = type.arraySizeExp.isConstant;
791                   char expression[10240];
792                   expression[0] = '\0';
793                   type.arraySizeExp.expType = null;
794                   yylloc = type.arraySizeExp.loc;
795                   if(inCompiler)
796                      PrintExpression(type.arraySizeExp, expression);
797                   Compiler_Error($"Array size not constant int (%s)\n", expression);
798                   yylloc = oldLoc;
799                }
800                GetInt(type.arraySizeExp, &type.arraySize);
801             }
802             else if(type.enumClass)
803             {
804                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
805                {
806                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
807                }
808                else
809                   type.arraySize = 0;
810             }
811             else
812             {
813                // Unimplemented auto size
814                type.arraySize = 0;
815             }
816
817             size = ComputeTypeSize(type.type) * type.arraySize;
818             if(type.type)
819             {
820                type.alignment = type.type.alignment;
821                type.pointerAlignment = type.type.pointerAlignment;
822             }
823
824             break;
825          case structType:
826          {
827             if(!type.members.first && type.enumName)
828             {
829                Symbol symbol = FindStruct(curContext, type.enumName);
830                if(symbol && symbol.type)
831                {
832                   ComputeTypeSize(symbol.type);
833                   size = symbol.type.size;
834                }
835             }
836             else
837             {
838                Type member;
839                for(member = type.members.first; member; member = member.next)
840                {
841                   uint addSize = ComputeTypeSize(member);
842
843                   member.offset = size;
844                   if(member.alignment && size % member.alignment)
845                      member.offset += member.alignment - (size % member.alignment);
846                   size = member.offset;
847
848                   if(member.pointerAlignment && type.size <= 4)
849                      type.pointerAlignment = true;
850                   else if(!member.pointerAlignment && member.alignment >= 8)
851                      type.pointerAlignment = false;
852
853                   type.alignment = Max(type.alignment, member.alignment);
854                   size += addSize;
855                }
856                if(type.alignment && size % type.alignment)
857                   size += type.alignment - (size % type.alignment);
858             }
859             break;
860          }
861          case unionType:
862          {
863             if(!type.members.first && type.enumName)
864             {
865                Symbol symbol = FindStruct(curContext, type.enumName);
866                if(symbol && symbol.type)
867                {
868                   ComputeTypeSize(symbol.type);
869                   size = symbol.type.size;
870                   type.alignment = symbol.type.alignment;
871                }
872             }
873             else
874             {
875                Type member;
876                for(member = type.members.first; member; member = member.next)
877                {
878                   uint addSize = ComputeTypeSize(member);
879
880                   member.offset = size;
881                   if(member.alignment && size % member.alignment)
882                      member.offset += member.alignment - (size % member.alignment);
883                   size = member.offset;
884
885                   if(member.pointerAlignment && type.size <= 4)
886                      type.pointerAlignment = true;
887                   else if(!member.pointerAlignment && member.alignment >= 8)
888                      type.pointerAlignment = false;
889
890                   type.alignment = Max(type.alignment, member.alignment);
891
892                   size = Max(size, addSize);
893                }
894                if(type.alignment && size % type.alignment)
895                   size += type.alignment - (size % type.alignment);
896             }
897             break;
898          }
899          case templateType:
900          {
901             TemplateParameter param = type.templateParameter;
902             Type baseType = ProcessTemplateParameterType(param);
903             if(baseType)
904             {
905                size = ComputeTypeSize(baseType);
906                type.alignment = baseType.alignment;
907                type.pointerAlignment = baseType.pointerAlignment;
908             }
909             else
910                type.alignment = size = sizeof(uint64);
911             break;
912          }
913          case enumType:
914          {
915             type.alignment = size = sizeof(enum { test });
916             break;
917          }
918          case thisClassType:
919          {
920             type.alignment = size = targetBits / 8; //sizeof(void *);
921             type.pointerAlignment = true;
922             break;
923          }
924       }
925       type.size = size;
926       type.computing = false;
927    }
928    return size;
929 }
930
931
932 /*static */int AddMembers(External neededBy, OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
933 {
934    // This function is in need of a major review when implementing private members etc.
935    DataMember topMember = isMember ? (DataMember) _class : null;
936    uint totalSize = 0;
937    uint maxSize = 0;
938    int alignment;
939    uint size;
940    DataMember member;
941    int anonID = 1;
942    Context context = isMember ? null : SetupTemplatesContext(_class);
943    if(addedPadding)
944       *addedPadding = false;
945
946    if(!isMember && _class.base)
947    {
948       maxSize = _class.structSize;
949       //if(_class.base.type != systemClass) // Commented out with new Instance _class
950       {
951          // DANGER: Testing this noHeadClass here...
952          if(_class.type == structClass || _class.type == noHeadClass)
953             /*totalSize = */AddMembers(neededBy, declarations, _class.base, false, &totalSize, topClass, null);
954          else
955          {
956             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
957             if(maxSize > baseSize)
958                maxSize -= baseSize;
959             else
960                maxSize = 0;
961          }
962       }
963    }
964
965    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
966    {
967       if(!member.isProperty)
968       {
969          switch(member.type)
970          {
971             case normalMember:
972             {
973                if(member.dataTypeString)
974                {
975                   OldList * specs = MkList(), * decls = MkList();
976                   Declarator decl;
977
978                   decl = SpecDeclFromString(member.dataTypeString, specs,
979                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
980                   ListAdd(decls, MkStructDeclarator(decl, null));
981                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
982
983                   if(!member.dataType)
984                      member.dataType = ProcessType(specs, decl);
985
986                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
987
988                   {
989                      Type type = ProcessType(specs, decl);
990                      DeclareType(neededBy, member.dataType, true, false);
991                      FreeType(type);
992                   }
993                   /*
994                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
995                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
996                      DeclareStruct(member.dataType._class.string, false);
997                   */
998
999                   ComputeTypeSize(member.dataType);
1000                   size = member.dataType.size;
1001                   alignment = member.dataType.alignment;
1002
1003                   if(alignment)
1004                   {
1005                      if(totalSize % alignment)
1006                         totalSize += alignment - (totalSize % alignment);
1007                   }
1008                   totalSize += size;
1009                }
1010                break;
1011             }
1012             case unionMember:
1013             case structMember:
1014             {
1015                OldList * specs = MkList(), * list = MkList();
1016                char id[100];
1017                sprintf(id, "__anon%d", anonID++);
1018
1019                size = 0;
1020                AddMembers(neededBy, list, (Class)member, true, &size, topClass, null);
1021                ListAdd(specs,
1022                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
1023                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, MkListOne(MkDeclaratorIdentifier(MkIdentifier(id))),null)));
1024                alignment = member.structAlignment;
1025
1026                if(alignment)
1027                {
1028                   if(totalSize % alignment)
1029                      totalSize += alignment - (totalSize % alignment);
1030                }
1031                totalSize += size;
1032                break;
1033             }
1034          }
1035       }
1036    }
1037    if(retSize)
1038    {
1039       if(topMember && topMember.type == unionMember)
1040          *retSize = Max(*retSize, totalSize);
1041       else
1042          *retSize += totalSize;
1043    }
1044    else if(totalSize < maxSize && _class.type != systemClass)
1045    {
1046       int autoPadding = 0;
1047       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
1048          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
1049       if(totalSize + autoPadding < maxSize)
1050       {
1051          char sizeString[50];
1052          sprintf(sizeString, "%d", maxSize - totalSize);
1053          ListAdd(declarations,
1054             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
1055             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
1056          if(addedPadding)
1057             *addedPadding = true;
1058       }
1059    }
1060    if(context)
1061       FinishTemplatesContext(context);
1062    return topMember ? topMember.memberID : _class.memberID;
1063 }
1064
1065 static int DeclareMembers(External neededBy, Class _class, bool isMember)
1066 {
1067    DataMember topMember = isMember ? (DataMember) _class : null;
1068    DataMember member;
1069    Context context = isMember ? null : SetupTemplatesContext(_class);
1070
1071    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1072       DeclareMembers(neededBy, _class.base, false);
1073
1074    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1075    {
1076       if(!member.isProperty)
1077       {
1078          switch(member.type)
1079          {
1080             case normalMember:
1081             {
1082                if(!member.dataType && member.dataTypeString)
1083                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1084                if(member.dataType)
1085                   DeclareType(neededBy, member.dataType, true, false);
1086                break;
1087             }
1088             case unionMember:
1089             case structMember:
1090             {
1091                DeclareMembers(neededBy, (Class)member, true);
1092                break;
1093             }
1094          }
1095       }
1096    }
1097    if(context)
1098       FinishTemplatesContext(context);
1099
1100    return topMember ? topMember.memberID : _class.memberID;
1101 }
1102
1103 static void IdentifyAnonStructs(OldList/*<ClassDef>*/ *  definitions)
1104 {
1105    ClassDef def;
1106    int anonID = 1;
1107    for(def = definitions->first; def; def = def.next)
1108    {
1109       if(def.type == declarationClassDef)
1110       {
1111          Declaration decl = def.decl;
1112          if(decl && decl.specifiers)
1113          {
1114             Specifier spec;
1115             bool isStruct = false;
1116             for(spec = decl.specifiers->first; spec; spec = spec.next)
1117             {
1118                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1119                {
1120                   if(spec.definitions)
1121                      IdentifyAnonStructs(spec.definitions);
1122                   isStruct = true;
1123                }
1124             }
1125             if(isStruct)
1126             {
1127                Declarator d = null;
1128                if(decl.declarators)
1129                {
1130                   for(d = decl.declarators->first; d; d = d.next)
1131                   {
1132                      Identifier idDecl = GetDeclId(d);
1133                      if(idDecl)
1134                         break;
1135                   }
1136                }
1137                if(!d)
1138                {
1139                   char id[100];
1140                   sprintf(id, "__anon%d", anonID++);
1141                   if(!decl.declarators)
1142                      decl.declarators = MkList();
1143                   ListAdd(decl.declarators, MkDeclaratorIdentifier(MkIdentifier(id)));
1144                }
1145             }
1146          }
1147       }
1148    }
1149 }
1150
1151 External DeclareStruct(External neededBy, const char * name, bool skipNoHead, bool needDereference)
1152 {
1153    return _DeclareStruct(neededBy, name, skipNoHead, needDereference, false);
1154 }
1155
1156 External _DeclareStruct(External neededBy, const char * name, bool skipNoHead, bool needDereference, bool fwdDecl)
1157 {
1158    External external = null;
1159    Symbol classSym = FindClass(name);
1160    OldList * curDeclarations = null;
1161
1162    if(!inCompiler || !classSym) return null;
1163
1164    // We don't need any declaration for bit classes...
1165    if(classSym.registered &&
1166       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1167       return null;
1168
1169    if(!classSym.registered || (classSym.registered.type == normalClass && classSym.registered.structSize && classSym.registered.base && classSym.registered.base.base))
1170       _DeclareStruct(neededBy, "ecere::com::Instance", false, true, fwdDecl);
1171
1172    external = classSym.structExternal;
1173
1174    if(external && external.declaration)
1175    {
1176       Specifier spec;
1177       for(spec = external.declaration.specifiers ? external.declaration.specifiers->first : null; spec; spec = spec.next)
1178          if(spec.type == structSpecifier || spec.type == unionSpecifier)
1179          {
1180             curDeclarations = spec.definitions;
1181             break;
1182          }
1183    }
1184
1185    if(classSym.registered && !classSym.declaring && classSym.imported && (!classSym.declaredStructSym || (classSym.registered.type == noHeadClass && !skipNoHead && external && !curDeclarations)))
1186    {
1187       OldList * specifiers, * declarators;
1188       OldList * declarations = null;
1189       char structName[1024];
1190       bool addedPadding = false;
1191       Specifier curSpec = null;
1192
1193       classSym.declaring++;
1194
1195       if(strchr(classSym.string, '<'))
1196       {
1197          if(classSym.registered.templateClass)
1198             external = _DeclareStruct(neededBy, classSym.registered.templateClass.fullName, skipNoHead, needDereference, fwdDecl);
1199          classSym.declaring--;
1200          return external;
1201       }
1202
1203       structName[0] = 0;
1204       FullClassNameCat(structName, name, false);
1205
1206       classSym.declaredStructSym = true;
1207       if(!external || (classSym.registered.type == noHeadClass && !skipNoHead && !curDeclarations))
1208       {
1209          bool add = false;
1210          if(!external)
1211          {
1212             external = MkExternalDeclaration(null);
1213             classSym.structExternal = external;
1214             external.symbol = classSym;
1215
1216             add = true;
1217          }
1218
1219          if(!skipNoHead)
1220          {
1221             declarations = MkList();
1222             AddMembers(external, declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1223          }
1224
1225          if(external.declaration)
1226          {
1227             Specifier spec;
1228             for(spec = external.declaration.specifiers ? external.declaration.specifiers->first : null; spec; spec = spec.next)
1229                if(spec.type == structSpecifier || spec.type == unionSpecifier)
1230                {
1231                   curSpec = spec;
1232                   curDeclarations = spec.definitions;
1233                   break;
1234                }
1235          }
1236
1237          if(declarations && (!declarations->count || (declarations->count == 1 && addedPadding)))
1238          {
1239             FreeList(declarations, FreeClassDef);
1240             declarations = null;
1241          }
1242
1243          if(classSym.registered.type != noHeadClass && !declarations)
1244          {
1245             FreeExternal(external);
1246             external = null;
1247             classSym.structExternal = null;
1248          }
1249          else
1250          {
1251             if(curSpec)
1252                curSpec.definitions = declarations;
1253             else
1254             {
1255                specifiers = MkList();
1256                declarators = MkList();
1257                ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1258                external.declaration = MkDeclaration(specifiers, declarators);
1259             }
1260             if(add)
1261                ast->Add(external);
1262          }
1263       }
1264       classSym.declaring--;
1265    }
1266    else if(!classSym.declaredStructSym && classSym.structExternal)
1267    {
1268       classSym.declaredStructSym = true;
1269
1270       if(classSym.registered)
1271          DeclareMembers(classSym.structExternal, classSym.registered, false);
1272
1273       if(classSym.structExternal.declaration && classSym.structExternal.declaration.specifiers)
1274       {
1275          Specifier spec;
1276          for(spec = classSym.structExternal.declaration.specifiers->first; spec; spec = spec.next)
1277          {
1278             if(spec.definitions)
1279                IdentifyAnonStructs(spec.definitions);
1280          }
1281       }
1282    }
1283    if(inCompiler && neededBy && (external || !classSym.imported))
1284    {
1285       if(!external)
1286       {
1287          classSym.structExternal = external = MkExternalDeclaration(null);
1288          external.symbol = classSym;
1289          ast->Add(external);
1290       }
1291       if(reachedPass15 && !external.declaration && classSym.registered && classSym.registered.type == noHeadClass)
1292       {
1293          // Declare nohead classes without definitions here (e.g. IteratorPointer)
1294          char structName[1024];
1295          OldList * specifiers, * declarators;
1296          structName[0] = 0;
1297          FullClassNameCat(structName, name, false);
1298          specifiers = MkList();
1299          declarators = MkList();
1300          ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), null));
1301          external.declaration = MkDeclaration(specifiers, declarators);
1302       }
1303       if(fwdDecl)
1304       {
1305          External e = external.fwdDecl ? external.fwdDecl : external;
1306          if(e.incoming.count)
1307             neededBy.CreateUniqueEdge(e, !needDereference && !external.fwdDecl);
1308       }
1309       else
1310          neededBy.CreateUniqueEdge(external, !needDereference);
1311    }
1312    return external;
1313 }
1314
1315 void DeclareProperty(External neededBy, Property prop, char * setName, char * getName)
1316 {
1317    Symbol symbol = prop.symbol;
1318    bool imported = false;
1319    bool dllImport = false;
1320    External structExternal = null;
1321    External instExternal = null;
1322
1323    strcpy(setName, "__ecereProp_");
1324    FullClassNameCat(setName, prop._class.fullName, false);
1325    strcat(setName, "_Set_");
1326    FullClassNameCat(setName, prop.name, true);
1327
1328    strcpy(getName, "__ecereProp_");
1329    FullClassNameCat(getName, prop._class.fullName, false);
1330    strcat(getName, "_Get_");
1331    FullClassNameCat(getName, prop.name, true);
1332
1333    if(!symbol || symbol._import)
1334    {
1335       if(!symbol)
1336       {
1337          Symbol classSym;
1338
1339          if(!prop._class.symbol)
1340             prop._class.symbol = FindClass(prop._class.fullName);
1341          classSym = prop._class.symbol;
1342          if(classSym && !classSym._import)
1343          {
1344             ModuleImport module;
1345
1346             if(prop._class.module)
1347                module = FindModule(prop._class.module);
1348             else
1349                module = mainModule;
1350
1351             classSym._import = ClassImport
1352             {
1353                name = CopyString(prop._class.fullName);
1354                isRemote = prop._class.isRemote;
1355             };
1356             module.classes.Add(classSym._import);
1357          }
1358          symbol = prop.symbol = Symbol { };
1359          symbol._import = (ClassImport)PropertyImport
1360          {
1361             name = CopyString(prop.name);
1362             isVirtual = false; //prop.isVirtual;
1363             hasSet = prop.Set ? true : false;
1364             hasGet = prop.Get ? true : false;
1365          };
1366          if(classSym)
1367             classSym._import.properties.Add(symbol._import);
1368       }
1369       imported = true;
1370       // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1371       if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1372          prop._class.module.importType != staticImport)
1373          dllImport = true;
1374    }
1375
1376    if(!symbol.type)
1377    {
1378       Context context = SetupTemplatesContext(prop._class);
1379       symbol.type = ProcessTypeString(prop.dataTypeString, false);
1380       FinishTemplatesContext(context);
1381    }
1382
1383    if((prop.Get && !symbol.externalGet) || (prop.Set && !symbol.externalSet))
1384    {
1385       if(prop._class.type == normalClass && prop._class.structSize)
1386          instExternal = DeclareStruct(null, "ecere::com::Instance", false, true);
1387       structExternal = DeclareStruct(null, prop._class.fullName, prop._class.type != structClass /*true*/, false);
1388    }
1389
1390    // Get
1391    if(prop.Get && !symbol.externalGet)
1392    {
1393       Declaration decl;
1394       OldList * specifiers, * declarators;
1395       Declarator d;
1396       OldList * params;
1397       Specifier spec = null;
1398       External external;
1399       Declarator typeDecl;
1400       bool simple = false;
1401       bool needReference;
1402
1403       specifiers = MkList();
1404       declarators = MkList();
1405       params = MkList();
1406
1407       ListAdd(params, MkTypeName(MkListOne(MkSpecifierName(prop._class.fullName)),
1408          MkDeclaratorIdentifier(MkIdentifier("this"))));
1409
1410       d = MkDeclaratorIdentifier(MkIdentifier(getName));
1411       //if(imported)
1412       if(dllImport)
1413          d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1414
1415       {
1416          Context context = SetupTemplatesContext(prop._class);
1417          typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1418          FinishTemplatesContext(context);
1419       }
1420
1421       // Make sure the simple _class's type is declared
1422       needReference = !typeDecl || typeDecl.type == identifierDeclarator;
1423       for(spec = specifiers->first; spec; spec = spec.next)
1424       {
1425          if(spec.type == nameSpecifier)
1426          {
1427             Symbol classSym = spec.symbol;
1428             if(needReference)
1429             {
1430                symbol._class = classSym.registered;
1431                if(classSym.registered && classSym.registered.type == structClass)
1432                   simple = true;
1433             }
1434             break;
1435          }
1436       }
1437
1438       if(!simple)
1439          d = PlugDeclarator(typeDecl, d);
1440       else
1441       {
1442          ListAdd(params, MkTypeName(specifiers,
1443             PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1444          specifiers = MkList();
1445       }
1446
1447       d = MkDeclaratorFunction(d, params);
1448
1449       //if(imported)
1450       if(dllImport)
1451          specifiers->Insert(null, MkSpecifier(EXTERN));
1452       else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1453          specifiers->Insert(null, MkSpecifier(STATIC));
1454       if(simple)
1455          ListAdd(specifiers, MkSpecifier(VOID));
1456
1457       ListAdd(declarators, MkInitDeclarator(d, null));
1458
1459       decl = MkDeclaration(specifiers, declarators);
1460
1461       external = MkExternalDeclaration(decl);
1462
1463       if(structExternal)
1464          external.CreateEdge(structExternal, false);
1465       if(instExternal)
1466          external.CreateEdge(instExternal, false);
1467
1468       if(spec)
1469          DeclareStruct(external, spec.name, false, needReference);
1470
1471       ast->Add(external);
1472       external.symbol = symbol;
1473       symbol.externalGet = external;
1474
1475       ReplaceThisClassSpecifiers(specifiers, prop._class);
1476
1477       if(typeDecl)
1478          FreeDeclarator(typeDecl);
1479    }
1480
1481    // Set
1482    if(prop.Set && !symbol.externalSet)
1483    {
1484       Declaration decl;
1485       OldList * specifiers, * declarators;
1486       Declarator d;
1487       OldList * params;
1488       Specifier spec = null;
1489       External external;
1490       Declarator typeDecl;
1491       bool needReference;
1492
1493       declarators = MkList();
1494       params = MkList();
1495
1496       if(!prop.conversion || prop._class.type == structClass)
1497       {
1498          ListAdd(params, MkTypeName(MkListOne(MkSpecifierName(prop._class.fullName)),
1499             MkDeclaratorIdentifier(MkIdentifier("this"))));
1500       }
1501
1502       specifiers = MkList();
1503
1504       {
1505          Context context = SetupTemplatesContext(prop._class);
1506          typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1507             MkDeclaratorIdentifier(MkIdentifier("value")));
1508          FinishTemplatesContext(context);
1509       }
1510       if(!strcmp(prop._class.base.fullName, "eda::Row") || !strcmp(prop._class.base.fullName, "eda::Id"))
1511          specifiers->Insert(null, MkSpecifier(CONST));
1512
1513       ListAdd(params, MkTypeName(specifiers, d));
1514
1515       d = MkDeclaratorIdentifier(MkIdentifier(setName));
1516       if(dllImport)
1517          d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1518       d = MkDeclaratorFunction(d, params);
1519
1520       // Make sure the simple _class's type is declared
1521       needReference = !typeDecl || typeDecl.type == identifierDeclarator;
1522       for(spec = specifiers->first; spec; spec = spec.next)
1523       {
1524          if(spec.type == nameSpecifier)
1525          {
1526             Symbol classSym = spec.symbol;
1527             if(needReference)
1528                symbol._class = classSym.registered;
1529             break;
1530          }
1531       }
1532
1533       ListAdd(declarators, MkInitDeclarator(d, null));
1534
1535       specifiers = MkList();
1536       if(dllImport)
1537          specifiers->Insert(null, MkSpecifier(EXTERN));
1538       else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1539          specifiers->Insert(null, MkSpecifier(STATIC));
1540
1541       if(!prop.conversion || prop._class.type == structClass)
1542          ListAdd(specifiers, MkSpecifier(VOID));
1543       else
1544          ListAdd(specifiers, MkSpecifierName(prop._class.fullName));
1545
1546       decl = MkDeclaration(specifiers, declarators);
1547
1548       external = MkExternalDeclaration(decl);
1549
1550       if(structExternal)
1551          external.CreateEdge(structExternal, false);
1552       if(instExternal)
1553          external.CreateEdge(instExternal, false);
1554
1555       if(spec)
1556          DeclareStruct(external, spec.name, false, needReference);
1557
1558       ast->Add(external);
1559       external.symbol = symbol;
1560       symbol.externalSet = external;
1561
1562       ReplaceThisClassSpecifiers(specifiers, prop._class);
1563    }
1564
1565    // Property (for Watchers)
1566    if(!symbol.externalPtr)
1567    {
1568       Declaration decl;
1569       External external;
1570       OldList * specifiers = MkList();
1571       char propName[1024];
1572
1573       if(imported)
1574          specifiers->Insert(null, MkSpecifier(EXTERN));
1575       else
1576       {
1577          specifiers->Insert(null, MkSpecifier(STATIC));
1578          specifiers->Add(MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
1579       }
1580
1581       ListAdd(specifiers, MkSpecifierName("Property"));
1582
1583       strcpy(propName, "__ecereProp_");
1584       FullClassNameCat(propName, prop._class.fullName, false);
1585       strcat(propName, "_");
1586       FullClassNameCat(propName, prop.name, true);
1587
1588       {
1589          OldList * list = MkList();
1590          ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1591
1592          if(!imported)
1593          {
1594             strcpy(propName, "__ecerePropM_");
1595             FullClassNameCat(propName, prop._class.fullName, false);
1596             strcat(propName, "_");
1597             FullClassNameCat(propName, prop.name, true);
1598
1599             ListAdd(list, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(propName)), null));
1600          }
1601          decl = MkDeclaration(specifiers, list);
1602       }
1603
1604       external = MkExternalDeclaration(decl);
1605       ast->Insert(curExternal.prev, external);
1606       external.symbol = symbol;
1607       symbol.externalPtr = external;
1608    }
1609
1610    if(inCompiler && neededBy)
1611    {
1612       // Could improve this to create edge on only what is needed...
1613       if(symbol.externalPtr)
1614          neededBy.CreateUniqueEdge(symbol.externalPtr, false);
1615
1616       if(symbol.externalGet)
1617          neededBy.CreateUniqueEdge(symbol.externalGet, symbol.externalGet.type == functionExternal);
1618
1619       if(symbol.externalSet)
1620          neededBy.CreateUniqueEdge(symbol.externalSet, symbol.externalSet.type == functionExternal);
1621
1622       // IsSet ?
1623    }
1624 }
1625
1626 // ***************** EXPRESSION PROCESSING ***************************
1627 public Type Dereference(Type source)
1628 {
1629    Type type = null;
1630    if(source)
1631    {
1632       if(source.kind == pointerType || source.kind == arrayType)
1633       {
1634          type = source.type;
1635          source.type.refCount++;
1636       }
1637       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1638       {
1639          type = Type
1640          {
1641             kind = charType;
1642             refCount = 1;
1643          };
1644       }
1645       // Support dereferencing of no head classes for now...
1646       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1647       {
1648          type = source;
1649          source.refCount++;
1650       }
1651       else
1652          Compiler_Error($"cannot dereference type\n");
1653    }
1654    return type;
1655 }
1656
1657 static Type Reference(Type source)
1658 {
1659    Type type = null;
1660    if(source)
1661    {
1662       type = Type
1663       {
1664          kind = pointerType;
1665          type = source;
1666          refCount = 1;
1667       };
1668       source.refCount++;
1669    }
1670    return type;
1671 }
1672
1673 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1674 {
1675    Identifier ident = member.identifiers ? member.identifiers->first : null;
1676    bool found = false;
1677    DataMember dataMember = null;
1678    Method method = null;
1679    bool freeType = false;
1680
1681    yylloc = member.loc;
1682
1683    if(!ident)
1684    {
1685       if(curMember)
1686       {
1687          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1688          if(*curMember)
1689          {
1690             found = true;
1691             dataMember = *curMember;
1692          }
1693       }
1694    }
1695    else
1696    {
1697       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1698       DataMember _subMemberStack[256];
1699       int _subMemberStackPos = 0;
1700
1701       // FILL MEMBER STACK
1702       if(!thisMember)
1703          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1704       if(thisMember)
1705       {
1706          dataMember = thisMember;
1707          if(curMember && thisMember.memberAccess == publicAccess)
1708          {
1709             *curMember = thisMember;
1710             *curClass = thisMember._class;
1711             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1712             *subMemberStackPos = _subMemberStackPos;
1713          }
1714          found = true;
1715       }
1716       else
1717       {
1718          // Setting a method
1719          method = eClass_FindMethod(_class, ident.string, privateModule);
1720          if(method && method.type == virtualMethod)
1721             found = true;
1722          else
1723             method = null;
1724       }
1725    }
1726
1727    if(found)
1728    {
1729       Type type = null;
1730       if(dataMember)
1731       {
1732          if(!dataMember.dataType && dataMember.dataTypeString)
1733          {
1734             //Context context = SetupTemplatesContext(dataMember._class);
1735             Context context = SetupTemplatesContext(_class);
1736             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1737             FinishTemplatesContext(context);
1738          }
1739          type = dataMember.dataType;
1740       }
1741       else if(method)
1742       {
1743          // This is for destination type...
1744          if(!method.dataType)
1745             ProcessMethodType(method);
1746          //DeclareMethod(method);
1747          // method.dataType = ((Symbol)method.symbol)->type;
1748          type = method.dataType;
1749       }
1750
1751       if(ident && ident.next)
1752       {
1753          for(ident = ident.next; ident && type; ident = ident.next)
1754          {
1755             if(type.kind == classType)
1756             {
1757                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1758                if(!dataMember)
1759                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1760                if(dataMember)
1761                   type = dataMember.dataType;
1762             }
1763             else if(type.kind == structType || type.kind == unionType)
1764             {
1765                Type memberType;
1766                for(memberType = type.members.first; memberType; memberType = memberType.next)
1767                {
1768                   if(!strcmp(memberType.name, ident.string))
1769                   {
1770                      type = memberType;
1771                      break;
1772                   }
1773                }
1774             }
1775          }
1776       }
1777
1778       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1779       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1780       {
1781          int id = 0;
1782          ClassTemplateParameter curParam = null;
1783          Class sClass;
1784          for(sClass = _class; sClass; sClass = sClass.base)
1785          {
1786             id = 0;
1787             if(sClass.templateClass) sClass = sClass.templateClass;
1788             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1789             {
1790                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1791                {
1792                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1793                   {
1794                      if(sClass.templateClass) sClass = sClass.templateClass;
1795                      id += sClass.templateParams.count;
1796                   }
1797                   break;
1798                }
1799                id++;
1800             }
1801             if(curParam) break;
1802          }
1803
1804          if(curParam)
1805          {
1806             ClassTemplateArgument arg = _class.templateArgs[id];
1807             if(arg.dataTypeString)
1808             {
1809                bool constant = type.constant;
1810                // FreeType(type);
1811                type = ProcessTypeString(arg.dataTypeString, false);
1812                if(type.kind == classType && constant) type.constant = true;
1813                else if(type.kind == pointerType)
1814                {
1815                   Type t = type.type;
1816                   while(t.kind == pointerType) t = t.type;
1817                   if(constant) t.constant = constant;
1818                }
1819                freeType = true;
1820                if(type && _class.templateClass)
1821                   type.passAsTemplate = true;
1822                if(type)
1823                {
1824                   // type.refCount++;
1825                   /*if(!exp.destType)
1826                   {
1827                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1828                      exp.destType.refCount++;
1829                   }*/
1830                }
1831             }
1832          }
1833       }
1834       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1835       {
1836          Class expClass = type._class.registered;
1837          Class cClass = null;
1838          int paramCount = 0;
1839          int lastParam = -1;
1840
1841          char templateString[1024];
1842          ClassTemplateParameter param;
1843          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1844          for(cClass = expClass; cClass; cClass = cClass.base)
1845          {
1846             int p = 0;
1847             if(cClass.templateClass) cClass = cClass.templateClass;
1848             for(param = cClass.templateParams.first; param; param = param.next)
1849             {
1850                int id = p;
1851                Class sClass;
1852                ClassTemplateArgument arg;
1853                for(sClass = cClass.base; sClass; sClass = sClass.base)
1854                {
1855                   if(sClass.templateClass) sClass = sClass.templateClass;
1856                   id += sClass.templateParams.count;
1857                }
1858                arg = expClass.templateArgs[id];
1859
1860                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1861                {
1862                   ClassTemplateParameter cParam;
1863                   //int p = numParams - sClass.templateParams.count;
1864                   int p = 0;
1865                   Class nextClass;
1866                   if(sClass.templateClass) sClass = sClass.templateClass;
1867
1868                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1869                   {
1870                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1871                      p += nextClass.templateParams.count;
1872                   }
1873
1874                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1875                   {
1876                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1877                      {
1878                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1879                         {
1880                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1881                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1882                            break;
1883                         }
1884                      }
1885                   }
1886                }
1887
1888                {
1889                   char argument[256];
1890                   argument[0] = '\0';
1891                   /*if(arg.name)
1892                   {
1893                      strcat(argument, arg.name.string);
1894                      strcat(argument, " = ");
1895                   }*/
1896                   switch(param.type)
1897                   {
1898                      case expression:
1899                      {
1900                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1901                         char expString[1024];
1902                         OldList * specs = MkList();
1903                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1904                         Expression exp;
1905                         char * string = PrintHexUInt64(arg.expression.ui64);
1906                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1907                         delete string;
1908
1909                         ProcessExpressionType(exp);
1910                         ComputeExpression(exp);
1911                         expString[0] = '\0';
1912                         PrintExpression(exp, expString);
1913                         strcat(argument, expString);
1914                         //delete exp;
1915                         FreeExpression(exp);
1916                         break;
1917                      }
1918                      case identifier:
1919                      {
1920                         strcat(argument, arg.member.name);
1921                         break;
1922                      }
1923                      case TemplateParameterType::type:
1924                      {
1925                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1926                            strcat(argument, arg.dataTypeString);
1927                         break;
1928                      }
1929                   }
1930                   if(argument[0])
1931                   {
1932                      if(paramCount) strcat(templateString, ", ");
1933                      if(lastParam != p - 1)
1934                      {
1935                         strcat(templateString, param.name);
1936                         strcat(templateString, " = ");
1937                      }
1938                      strcat(templateString, argument);
1939                      paramCount++;
1940                      lastParam = p;
1941                   }
1942                   p++;
1943                }
1944             }
1945          }
1946          {
1947             int len = strlen(templateString);
1948             if(templateString[len-1] == '<')
1949                len--;
1950             else
1951             {
1952                if(templateString[len-1] == '>')
1953                   templateString[len++] = ' ';
1954                templateString[len++] = '>';
1955             }
1956             templateString[len++] = '\0';
1957          }
1958          {
1959             Context context = SetupTemplatesContext(_class);
1960             if(freeType) FreeType(type);
1961             type = ProcessTypeString(templateString, false);
1962             freeType = true;
1963             FinishTemplatesContext(context);
1964          }
1965       }
1966
1967       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1968       {
1969          ProcessExpressionType(member.initializer.exp);
1970          if(!member.initializer.exp.expType)
1971          {
1972             if(inCompiler)
1973             {
1974                char expString[10240];
1975                expString[0] = '\0';
1976                PrintExpression(member.initializer.exp, expString);
1977                ChangeCh(expString, '\n', ' ');
1978                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1979             }
1980          }
1981          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1982          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false, true))
1983          {
1984             Compiler_Error($"incompatible instance method %s\n", ident.string);
1985          }
1986       }
1987       else if(member.initializer)
1988       {
1989          /*
1990          FreeType(member.exp.destType);
1991          member.exp.destType = type;
1992          if(member.exp.destType)
1993             member.exp.destType.refCount++;
1994          ProcessExpressionType(member.exp);
1995          */
1996
1997          ProcessInitializer(member.initializer, type);
1998       }
1999       if(freeType) FreeType(type);
2000    }
2001    else
2002    {
2003       if(_class && _class.type == unitClass)
2004       {
2005          if(member.initializer)
2006          {
2007             /*
2008             FreeType(member.exp.destType);
2009             member.exp.destType = MkClassType(_class.fullName);
2010             ProcessExpressionType(member.initializer, type);
2011             */
2012             Type type = MkClassType(_class.fullName);
2013             ProcessInitializer(member.initializer, type);
2014             FreeType(type);
2015          }
2016       }
2017       else
2018       {
2019          if(member.initializer)
2020          {
2021             //ProcessExpressionType(member.exp);
2022             ProcessInitializer(member.initializer, null);
2023          }
2024          if(ident)
2025          {
2026             if(method)
2027             {
2028                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
2029             }
2030             else if(_class)
2031             {
2032                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
2033                if(inCompiler)
2034                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
2035             }
2036          }
2037          else if(_class)
2038             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
2039       }
2040    }
2041 }
2042
2043 void ProcessInstantiationType(Instantiation inst)
2044 {
2045    yylloc = inst.loc;
2046    if(inst._class)
2047    {
2048       MembersInit members;
2049       Symbol classSym;
2050       Class _class;
2051
2052       classSym = inst._class.symbol;
2053       _class = classSym ? classSym.registered : null;
2054
2055       if(!_class || _class.type != noHeadClass)
2056          DeclareStruct(curExternal, inst._class.name, false, true);
2057
2058       afterExternal = afterExternal ? afterExternal : curExternal;
2059
2060       if(inst.exp)
2061          ProcessExpressionType(inst.exp);
2062
2063       inst.isConstant = true;
2064       if(inst.members)
2065       {
2066          DataMember curMember = null;
2067          Class curClass = null;
2068          DataMember subMemberStack[256];
2069          int subMemberStackPos = 0;
2070
2071          for(members = inst.members->first; members; members = members.next)
2072          {
2073             switch(members.type)
2074             {
2075                case methodMembersInit:
2076                {
2077                   char name[1024];
2078                   static uint instMethodID = 0;
2079                   External external = curExternal;
2080                   Context context = curContext;
2081                   Declarator declarator = members.function.declarator;
2082                   Identifier nameID = GetDeclId(declarator);
2083                   char * unmangled = nameID ? nameID.string : null;
2084                   Expression exp;
2085                   External createdExternal = null;
2086
2087                   if(inCompiler)
2088                   {
2089                      char number[16];
2090                      strcpy(name, "__ecereInstMeth_");
2091                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
2092                      strcat(name, "_");
2093                      strcat(name, nameID.string);
2094                      strcat(name, "_");
2095                      sprintf(number, "_%08d", instMethodID++);
2096                      strcat(name, number);
2097                      nameID.string = CopyString(name);
2098                   }
2099
2100                   // Do modifications here...
2101                   if(declarator)
2102                   {
2103                      Symbol symbol = declarator.symbol;
2104                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2105
2106                      if(method && method.type == virtualMethod)
2107                      {
2108                         symbol.method = method;
2109                         ProcessMethodType(method);
2110
2111                         if(!symbol.type.thisClass)
2112                         {
2113                            if(method.dataType.thisClass && currentClass &&
2114                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2115                            {
2116                               if(!currentClass.symbol)
2117                                  currentClass.symbol = FindClass(currentClass.fullName);
2118                               symbol.type.thisClass = currentClass.symbol;
2119                            }
2120                            else
2121                            {
2122                               if(!_class.symbol)
2123                                  _class.symbol = FindClass(_class.fullName);
2124                               symbol.type.thisClass = _class.symbol;
2125                            }
2126                         }
2127                         DeclareType(curExternal, symbol.type, true, true);
2128
2129                      }
2130                      else if(classSym)
2131                      {
2132                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2133                            unmangled, classSym.string);
2134                      }
2135                   }
2136
2137                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2138
2139                   if(nameID)
2140                   {
2141                      FreeSpecifier(nameID._class);
2142                      nameID._class = null;
2143                   }
2144
2145                   curExternal = createdExternal;
2146                   if(inCompiler)
2147                   {
2148                      if(createdExternal.function)
2149                         ProcessFunction(createdExternal.function);
2150                   }
2151                   else if(declarator)
2152                   {
2153                      curExternal = declarator.symbol.pointerExternal;
2154                      ProcessFunction((FunctionDefinition)members.function);
2155                   }
2156                   curExternal = external;
2157                   curContext = context;
2158
2159                   if(inCompiler)
2160                   {
2161                      FreeClassFunction(members.function);
2162
2163                      // In this pass, turn this into a MemberInitData
2164                      exp = QMkExpId(name);
2165                      members.type = dataMembersInit;
2166                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2167
2168                      delete unmangled;
2169                   }
2170                   break;
2171                }
2172                case dataMembersInit:
2173                {
2174                   if(members.dataMembers && classSym)
2175                   {
2176                      MemberInit member;
2177                      Location oldyyloc = yylloc;
2178                      for(member = members.dataMembers->first; member; member = member.next)
2179                      {
2180                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2181                         if(member.initializer && !member.initializer.isConstant)
2182                            inst.isConstant = false;
2183                      }
2184                      yylloc = oldyyloc;
2185                   }
2186                   break;
2187                }
2188             }
2189          }
2190       }
2191    }
2192 }
2193
2194 void DeclareType(External neededFor, Type type, bool needDereference, bool forFunctionDef)
2195 {
2196    _DeclareType(neededFor, type, needDereference, forFunctionDef, false);
2197 }
2198
2199 void DeclareTypeForwardDeclare(External neededFor, Type type, bool needDereference, bool forFunctionDef)
2200 {
2201    _DeclareType(neededFor, type, needDereference, forFunctionDef, true);
2202 }
2203
2204 static void _DeclareType(External neededFor, Type type, bool needDereference, bool forFunctionDef, bool fwdDecl)
2205 {
2206    if(inCompiler)
2207    {
2208       if(type.kind == functionType)
2209       {
2210          Type param;
2211          for(param = type.params.first; param; param = param.next)
2212             _DeclareType(neededFor, param, forFunctionDef, false, fwdDecl);
2213          _DeclareType(neededFor, type.returnType, forFunctionDef, false, fwdDecl);
2214       }
2215       else if(type.kind == pointerType)
2216          _DeclareType(neededFor, type.type, false, false, fwdDecl);
2217       else if(type.kind == classType)
2218       {
2219          Class c = type._class.registered;
2220          _DeclareStruct(neededFor, c ? c.fullName : "ecere::com::Instance", c ? c.type == noHeadClass : false, needDereference && c && c.type == structClass, fwdDecl);
2221       }
2222       else if(type.kind == structType || type.kind == unionType)
2223       {
2224          Type member;
2225          for(member = type.members.first; member; member = member.next)
2226             _DeclareType(neededFor, member, needDereference, forFunctionDef, fwdDecl);
2227       }
2228       else if(type.kind == arrayType)
2229          _DeclareType(neededFor, type.arrayType, true, false, fwdDecl);
2230    }
2231 }
2232
2233 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2234 {
2235    ClassTemplateArgument * arg = null;
2236    int id = 0;
2237    ClassTemplateParameter curParam = null;
2238    Class sClass;
2239    for(sClass = _class; sClass; sClass = sClass.base)
2240    {
2241       id = 0;
2242       if(sClass.templateClass) sClass = sClass.templateClass;
2243       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2244       {
2245          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2246          {
2247             for(sClass = sClass.base; sClass; sClass = sClass.base)
2248             {
2249                if(sClass.templateClass) sClass = sClass.templateClass;
2250                id += sClass.templateParams.count;
2251             }
2252             break;
2253          }
2254          id++;
2255       }
2256       if(curParam) break;
2257    }
2258    if(curParam)
2259    {
2260       arg = &_class.templateArgs[id];
2261       if(arg && param.type == type)
2262          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2263    }
2264    return arg;
2265 }
2266
2267 public Context SetupTemplatesContext(Class _class)
2268 {
2269    Context context = PushContext();
2270    context.templateTypesOnly = true;
2271    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2272    {
2273       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2274       for(; param; param = param.next)
2275       {
2276          if(param.type == type && param.identifier)
2277          {
2278             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2279             curContext.templateTypes.Add((BTNode)type);
2280          }
2281       }
2282    }
2283    else if(_class)
2284    {
2285       Class sClass;
2286       for(sClass = _class; sClass; sClass = sClass.base)
2287       {
2288          ClassTemplateParameter p;
2289          for(p = sClass.templateParams.first; p; p = p.next)
2290          {
2291             //OldList * specs = MkList();
2292             //Declarator decl = null;
2293             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2294             if(p.type == type)
2295             {
2296                TemplateParameter param = p.param;
2297                TemplatedType type;
2298                if(!param)
2299                {
2300                   // ADD DATA TYPE HERE...
2301                   p.param = param = TemplateParameter
2302                   {
2303                      identifier = MkIdentifier(p.name), type = p.type,
2304                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2305                   };
2306                }
2307                type = TemplatedType { key = (uintptr)p.name, param = param };
2308                curContext.templateTypes.Add((BTNode)type);
2309             }
2310          }
2311       }
2312    }
2313    return context;
2314 }
2315
2316 public void FinishTemplatesContext(Context context)
2317 {
2318    PopContext(context);
2319    FreeContext(context);
2320    delete context;
2321 }
2322
2323 public void ProcessMethodType(Method method)
2324 {
2325    if(!method.dataType)
2326    {
2327       Context context = SetupTemplatesContext(method._class);
2328
2329       method.dataType = ProcessTypeString(method.dataTypeString, false);
2330
2331       FinishTemplatesContext(context);
2332
2333       if(method.type != virtualMethod && method.dataType)
2334       {
2335          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2336          {
2337             if(!method._class.symbol)
2338                method._class.symbol = FindClass(method._class.fullName);
2339             method.dataType.thisClass = method._class.symbol;
2340          }
2341       }
2342
2343       // Why was this commented out? Working fine without now...
2344
2345       /*
2346       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2347          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2348          */
2349    }
2350
2351    /*
2352    if(type)
2353    {
2354       char * par = strstr(type, "(");
2355       char * classOp = null;
2356       int classOpLen = 0;
2357       if(par)
2358       {
2359          int c;
2360          for(c = par-type-1; c >= 0; c++)
2361          {
2362             if(type[c] == ':' && type[c+1] == ':')
2363             {
2364                classOp = type + c - 1;
2365                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2366                {
2367                   classOp--;
2368                   classOpLen++;
2369                }
2370                break;
2371             }
2372             else if(!isspace(type[c]))
2373                break;
2374          }
2375       }
2376       if(classOp)
2377       {
2378          char temp[1024];
2379          int typeLen = strlen(type);
2380          memcpy(temp, classOp, classOpLen);
2381          temp[classOpLen] = '\0';
2382          if(temp[0])
2383             _class = eSystem_FindClass(module, temp);
2384          else
2385             _class = null;
2386          method.dataTypeString = new char[typeLen - classOpLen + 1];
2387          memcpy(method.dataTypeString, type, classOp - type);
2388          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2389       }
2390       else
2391          method.dataTypeString = type;
2392    }
2393    */
2394 }
2395
2396
2397 public void ProcessPropertyType(Property prop)
2398 {
2399    if(!prop.dataType)
2400    {
2401       Context context = SetupTemplatesContext(prop._class);
2402       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2403       FinishTemplatesContext(context);
2404    }
2405 }
2406
2407 public void DeclareMethod(External neededFor, Method method, const char * name)
2408 {
2409    Symbol symbol = method.symbol;
2410    if(!symbol || (!symbol.pointerExternal && (!symbol.methodCodeExternal || method.type == virtualMethod)))
2411    {
2412       bool dllImport = false;
2413
2414       if(!method.dataType)
2415          method.dataType = ProcessTypeString(method.dataTypeString, false);
2416
2417       //if(!symbol || symbol._import || method.type == virtualMethod)
2418       {
2419          if(!symbol || method.type == virtualMethod)
2420          {
2421             Symbol classSym;
2422             if(!method._class.symbol)
2423                method._class.symbol = FindClass(method._class.fullName);
2424             classSym = method._class.symbol;
2425             if(!classSym._import)
2426             {
2427                ModuleImport module;
2428
2429                if(method._class.module && method._class.module.name)
2430                   module = FindModule(method._class.module);
2431                else
2432                   module = mainModule;
2433                classSym._import = ClassImport
2434                {
2435                   name = CopyString(method._class.fullName);
2436                   isRemote = method._class.isRemote;
2437                };
2438                module.classes.Add(classSym._import);
2439             }
2440             if(!symbol)
2441             {
2442                symbol = method.symbol = Symbol { };
2443             }
2444             if(!symbol._import)
2445             {
2446                symbol._import = (ClassImport)MethodImport
2447                {
2448                   name = CopyString(method.name);
2449                   isVirtual = method.type == virtualMethod;
2450                };
2451                classSym._import.methods.Add(symbol._import);
2452             }
2453             if(!symbol)
2454             {
2455                symbol.type = method.dataType;
2456                if(symbol.type) symbol.type.refCount++;
2457             }
2458          }
2459          if(!method.dataType.dllExport)
2460          {
2461             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2462                dllImport = true;
2463          }
2464       }
2465
2466       if(inCompiler)
2467       {
2468          // We need a declaration here :)
2469          Declaration decl;
2470          OldList * specifiers, * declarators;
2471          Declarator d;
2472          Declarator funcDecl;
2473          External external;
2474
2475          specifiers = MkList();
2476          declarators = MkList();
2477
2478          if(dllImport)
2479             ListAdd(specifiers, MkSpecifier(EXTERN));
2480          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2481             ListAdd(specifiers, MkSpecifier(STATIC));
2482
2483          if(method.type == virtualMethod)
2484          {
2485             ListAdd(specifiers, MkSpecifier(INT));
2486             d = MkDeclaratorIdentifier(MkIdentifier(name));
2487          }
2488          else
2489          {
2490             d = MkDeclaratorIdentifier(MkIdentifier(name));
2491             if(dllImport)
2492                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2493             {
2494                Context context = SetupTemplatesContext(method._class);
2495                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2496                FinishTemplatesContext(context);
2497             }
2498             funcDecl = GetFuncDecl(d);
2499
2500             if(dllImport)
2501             {
2502                Specifier spec, next;
2503                for(spec = specifiers->first; spec; spec = next)
2504                {
2505                   next = spec.next;
2506                   if(spec.type == extendedSpecifier)
2507                   {
2508                      specifiers->Remove(spec);
2509                      FreeSpecifier(spec);
2510                   }
2511                }
2512             }
2513
2514             // Add this parameter if not a static method
2515             if(method.dataType && !method.dataType.staticMethod)
2516             {
2517                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2518                {
2519                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2520                   TypeName thisParam = MkTypeName(MkListOne(
2521                      MkSpecifierName(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2522                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2523                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2524                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2525
2526                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2527                   {
2528                      TypeName param = funcDecl.function.parameters->first;
2529                      funcDecl.function.parameters->Remove(param);
2530                      FreeTypeName(param);
2531                   }
2532
2533                   if(!funcDecl.function.parameters)
2534                      funcDecl.function.parameters = MkList();
2535                   funcDecl.function.parameters->Insert(null, thisParam);
2536                }
2537             }
2538          }
2539          ProcessDeclarator(d, true);
2540
2541          ListAdd(declarators, MkInitDeclarator(d, null));
2542
2543          decl = MkDeclaration(specifiers, declarators);
2544
2545          ReplaceThisClassSpecifiers(specifiers, method._class);
2546
2547          external = MkExternalDeclaration(decl);
2548          external.symbol = symbol;
2549          symbol.pointerExternal = external;
2550          ast->Add(external);
2551          DeclareStruct(external, method._class.fullName, true, true);
2552          if(method.dataType)
2553             DeclareType(external, method.dataType, true, true);
2554       }
2555    }
2556    if(inCompiler && neededFor)
2557    {
2558       External external = symbol.pointerExternal ? symbol.pointerExternal : symbol.methodCodeExternal;
2559       neededFor.CreateUniqueEdge(external, external.type == functionExternal);
2560    }
2561 }
2562
2563 char * ReplaceThisClass(Class _class)
2564 {
2565    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2566    {
2567       bool first = true;
2568       int p = 0;
2569       ClassTemplateParameter param;
2570       int lastParam = -1;
2571
2572       char className[1024];
2573       strcpy(className, _class.fullName);
2574       for(param = _class.templateParams.first; param; param = param.next)
2575       {
2576          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2577          {
2578             if(first) strcat(className, "<");
2579             if(!first) strcat(className, ", ");
2580             if(lastParam + 1 != p)
2581             {
2582                strcat(className, param.name);
2583                strcat(className, " = ");
2584             }
2585             strcat(className, param.name);
2586             first = false;
2587             lastParam = p;
2588          }
2589          p++;
2590       }
2591       if(!first)
2592       {
2593          int len = strlen(className);
2594          if(className[len-1] == '>') className[len++] = ' ';
2595          className[len++] = '>';
2596          className[len++] = '\0';
2597       }
2598       return CopyString(className);
2599    }
2600    else
2601       return CopyString(_class.fullName);
2602 }
2603
2604 Type ReplaceThisClassType(Class _class)
2605 {
2606    Type type;
2607    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2608    {
2609       bool first = true;
2610       int p = 0;
2611       ClassTemplateParameter param;
2612       int lastParam = -1;
2613       char className[1024];
2614       strcpy(className, _class.fullName);
2615
2616       for(param = _class.templateParams.first; param; param = param.next)
2617       {
2618          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2619          {
2620             if(first) strcat(className, "<");
2621             if(!first) strcat(className, ", ");
2622             if(lastParam + 1 != p)
2623             {
2624                strcat(className, param.name);
2625                strcat(className, " = ");
2626             }
2627             strcat(className, param.name);
2628             first = false;
2629             lastParam = p;
2630          }
2631          p++;
2632       }
2633       if(!first)
2634       {
2635          int len = strlen(className);
2636          if(className[len-1] == '>') className[len++] = ' ';
2637          className[len++] = '>';
2638          className[len++] = '\0';
2639       }
2640       type = MkClassType(className);
2641       //type = ProcessTypeString(className, false);
2642    }
2643    else
2644    {
2645       type = MkClassType(_class.fullName);
2646       //type = ProcessTypeString(_class.fullName, false);
2647    }
2648    //type.wasThisClass = true;
2649    return type;
2650 }
2651
2652 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2653 {
2654    if(specs != null && _class)
2655    {
2656       Specifier spec;
2657       for(spec = specs.first; spec; spec = spec.next)
2658       {
2659          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2660          {
2661             spec.type = nameSpecifier;
2662             spec.name = ReplaceThisClass(_class);
2663             spec.symbol = FindClass(spec.name); //_class.symbol;
2664          }
2665       }
2666    }
2667 }
2668
2669 // Returns imported or not
2670 bool DeclareFunction(External neededFor, GlobalFunction function, char * name)
2671 {
2672    Symbol symbol = function.symbol;
2673    // TOCHECK: Might get rid of the pointerExternal check in favor of marking the edge as breakable
2674    if(!symbol || !symbol.pointerExternal)
2675    {
2676       bool imported = false;
2677       bool dllImport = false;
2678
2679       if(!function.dataType)
2680       {
2681          function.dataType = ProcessTypeString(function.dataTypeString, false);
2682          if(!function.dataType.thisClass)
2683             function.dataType.staticMethod = true;
2684       }
2685
2686       if(inCompiler)
2687       {
2688          if(!symbol)
2689          {
2690             ModuleImport module = FindModule(function.module);
2691             // WARNING: This is not added anywhere...
2692             symbol = function.symbol = Symbol {  };
2693
2694             if(module.name)
2695             {
2696                if(!function.dataType.dllExport)
2697                {
2698                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2699                   module.functions.Add(symbol._import);
2700                }
2701             }
2702             // Set the symbol type
2703             {
2704                symbol.type = ProcessTypeString(function.dataTypeString, false);
2705                if(!symbol.type.thisClass)
2706                   symbol.type.staticMethod = true;
2707             }
2708          }
2709          imported = symbol._import ? true : false;
2710          if(imported && function.module != privateModule && function.module.importType != staticImport)
2711             dllImport = true;
2712       }
2713
2714       if(inCompiler)
2715       {
2716          // TOCHECK: What's with the functionExternal check here? Is it Edge breaking / forward declaration?
2717          //if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2718          {
2719             // We need a declaration here :)
2720             Declaration decl;
2721             OldList * specifiers, * declarators;
2722             Declarator d;
2723             Declarator funcDecl;
2724             External external;
2725
2726             specifiers = MkList();
2727             declarators = MkList();
2728
2729             ListAdd(specifiers, MkSpecifier(EXTERN));
2730
2731             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2732             if(dllImport)
2733                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2734
2735             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2736             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2737             if(function.module.importType == staticImport)
2738             {
2739                Specifier spec;
2740                for(spec = specifiers->first; spec; spec = spec.next)
2741                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2742                   {
2743                      specifiers->Remove(spec);
2744                      FreeSpecifier(spec);
2745                      break;
2746                   }
2747             }
2748
2749             funcDecl = GetFuncDecl(d);
2750
2751             // Make sure we don't have empty parameter declarations for static methods...
2752             if(funcDecl && !funcDecl.function.parameters)
2753             {
2754                funcDecl.function.parameters = MkList();
2755                funcDecl.function.parameters->Insert(null,
2756                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2757             }
2758
2759             ListAdd(declarators, MkInitDeclarator(d, null));
2760
2761             {
2762                Context oldCtx = curContext;
2763                curContext = globalContext;
2764                decl = MkDeclaration(specifiers, declarators);
2765                curContext = oldCtx;
2766             }
2767
2768             // Keep a different symbol for the function definition than the declaration...
2769             /* Note: This should be handled by the edge breaking...
2770             if(symbol.pointerExternal && symbol.pointerExternal.type == functionExternal)
2771             {
2772                Symbol functionSymbol { };
2773                // Copy symbol
2774                {
2775                   *functionSymbol = *symbol;
2776                   functionSymbol.string = CopyString(symbol.string);
2777                   if(functionSymbol.type)
2778                      functionSymbol.type.refCount++;
2779                }
2780
2781                excludedSymbols->Add(functionSymbol);
2782
2783                symbol.pointerExternal.symbol = functionSymbol;
2784             }
2785             */
2786             external = MkExternalDeclaration(decl);
2787             ast->Add(external);
2788             external.symbol = symbol;
2789             symbol.pointerExternal = external;
2790
2791             DeclareType(external, function.dataType, true, true);
2792          }
2793       }
2794    }
2795    if(inCompiler && neededFor && symbol && symbol.pointerExternal)
2796       neededFor.CreateUniqueEdge(symbol.pointerExternal, symbol.pointerExternal.type == functionExternal);
2797    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2798 }
2799
2800 void DeclareGlobalData(External neededFor, GlobalData data)
2801 {
2802    Symbol symbol = data.symbol;
2803    // TOCHECK: Might get rid of the pointerExternal check in favor of marking the edge as breakable
2804    if(!symbol || !symbol.pointerExternal)
2805    {
2806       if(inCompiler)
2807       {
2808          if(!symbol)
2809             symbol = data.symbol = Symbol { };
2810       }
2811       if(!data.dataType)
2812          data.dataType = ProcessTypeString(data.dataTypeString, false);
2813
2814       if(inCompiler)
2815       {
2816          // We need a declaration here :)
2817          Declaration decl;
2818          OldList * specifiers, * declarators;
2819          Declarator d;
2820          External external;
2821
2822          specifiers = MkList();
2823          declarators = MkList();
2824
2825          ListAdd(specifiers, MkSpecifier(EXTERN));
2826          d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2827          d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2828
2829          ListAdd(declarators, MkInitDeclarator(d, null));
2830
2831          decl = MkDeclaration(specifiers, declarators);
2832          external = MkExternalDeclaration(decl);
2833          if(curExternal)
2834             ast->Insert(curExternal.prev, external);
2835          external.symbol = symbol;
2836          symbol.pointerExternal = external;
2837
2838          DeclareType(external, data.dataType, true, true);
2839       }
2840    }
2841    if(inCompiler && neededFor && symbol && symbol.pointerExternal)
2842       neededFor.CreateUniqueEdge(symbol.pointerExternal, false);
2843 }
2844
2845 class Conversion : struct
2846 {
2847    Conversion prev, next;
2848    Property convert;
2849    bool isGet;
2850    Type resultType;
2851 };
2852
2853 static bool CheckConstCompatibility(Type source, Type dest, bool warn)
2854 {
2855    bool status = true;
2856    if(((source.kind == classType && source._class && source._class.registered) || source.kind == arrayType || source.kind == pointerType) &&
2857       ((dest.kind == classType && dest._class && dest._class.registered) || /*dest.kind == arrayType || */dest.kind == pointerType))
2858    {
2859       Class sourceClass = source.kind == classType ? source._class.registered : null;
2860       Class destClass = dest.kind == classType ? dest._class.registered : null;
2861       if((!sourceClass || (sourceClass && sourceClass.type == normalClass && !sourceClass.structSize)) &&
2862          (!destClass || (destClass && destClass.type == normalClass && !destClass.structSize)))
2863       {
2864          Type sourceType = source, destType = dest;
2865          while((sourceType.kind == pointerType || sourceType.kind == arrayType) && sourceType.type) sourceType = sourceType.type;
2866          while((destType.kind == pointerType || destType.kind == arrayType) && destType.type) destType = destType.type;
2867          if(!destType.constant && sourceType.constant)
2868          {
2869             status = false;
2870             if(warn)
2871                Compiler_Warning($"discarding const qualifier\n");
2872          }
2873       }
2874    }
2875    return status;
2876 }
2877
2878 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams,
2879                        bool isConversionExploration, bool warnConst)
2880 {
2881    if(source && dest)
2882    {
2883       if(warnConst)
2884          CheckConstCompatibility(source, dest, true);
2885       // Property convert;
2886
2887       if(source.kind == templateType && dest.kind != templateType)
2888       {
2889          Type type = ProcessTemplateParameterType(source.templateParameter);
2890          if(type) source = type;
2891       }
2892
2893       if(dest.kind == templateType && source.kind != templateType)
2894       {
2895          Type type = ProcessTemplateParameterType(dest.templateParameter);
2896          if(type) dest = type;
2897       }
2898
2899       if(dest.classObjectType == typedObject && dest.kind != functionType)
2900       {
2901          if(source.classObjectType != anyObject)
2902             return true;
2903          else
2904          {
2905             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2906             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2907             {
2908                return true;
2909             }
2910          }
2911       }
2912       else
2913       {
2914          if(source.kind != functionType && source.classObjectType == anyObject)
2915             return true;
2916          if(dest.kind != functionType && dest.classObjectType == anyObject && source.classObjectType != typedObject)
2917             return true;
2918       }
2919
2920       if((dest.kind == structType && source.kind == structType) ||
2921          (dest.kind == unionType && source.kind == unionType))
2922       {
2923          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2924              (source.members.first && source.members.first == dest.members.first))
2925             return true;
2926       }
2927
2928       if(dest.kind == ellipsisType && source.kind != voidType)
2929          return true;
2930
2931       if(dest.kind == pointerType && dest.type.kind == voidType &&
2932          ((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))
2933          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2934
2935          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2936
2937          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2938          return true;
2939       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2940          ((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))
2941          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2942          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2943
2944          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2945          return true;
2946
2947       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2948       {
2949          if(source._class.registered && source._class.registered.type == unitClass)
2950          {
2951             if(conversions != null)
2952             {
2953                if(source._class.registered == dest._class.registered)
2954                   return true;
2955             }
2956             else
2957             {
2958                Class sourceBase, destBase;
2959                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2960                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2961                if(sourceBase == destBase)
2962                   return true;
2963             }
2964          }
2965          // Don't match enum inheriting from other enum if resolving enumeration values
2966          // TESTING: !dest.classObjectType
2967          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2968             (enumBaseType ||
2969                (!source._class.registered || source._class.registered.type != enumClass) ||
2970                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2971             return true;
2972          else
2973          {
2974             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2975             if(enumBaseType &&
2976                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2977                ((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)
2978             {
2979                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2980                {
2981                   return true;
2982                }
2983             }
2984          }
2985       }
2986
2987       // JUST ADDED THIS...
2988       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2989          return true;
2990
2991       if(doConversion)
2992       {
2993          // Just added this for Straight conversion of ColorAlpha => Color
2994          if(source.kind == classType)
2995          {
2996             Class _class;
2997             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2998             {
2999                Property convert;
3000                for(convert = _class.conversions.first; convert; convert = convert.next)
3001                {
3002                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3003                   {
3004                      Conversion after = (conversions != null) ? conversions.last : null;
3005
3006                      if(!convert.dataType)
3007                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3008                      // Only go ahead with this conversion flow while processing an existing conversion if the conversion data type is a class
3009                      if((!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3010                         MatchTypes(convert.dataType, dest, conversions, null, null,
3011                            (convert.dataType.kind == classType && !strcmp(convert.dataTypeString, "String")) ? true : false,
3012                               convert.dataType.kind == classType, false, true, warnConst))
3013                      {
3014                         if(!conversions && !convert.Get)
3015                            return true;
3016                         else if(conversions != null)
3017                         {
3018                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3019                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3020                               (dest.kind != classType || dest._class.registered != _class.base))
3021                               return true;
3022                            else
3023                            {
3024                               Conversion conv { convert = convert, isGet = true };
3025                               // conversions.Add(conv);
3026                               conversions.Insert(after, conv);
3027
3028                               return true;
3029                            }
3030                         }
3031                      }
3032                   }
3033                }
3034             }
3035          }
3036
3037          // MOVING THIS??
3038
3039          if(dest.kind == classType)
3040          {
3041             Class _class;
3042             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3043             {
3044                Property convert;
3045                for(convert = _class.conversions.first; convert; convert = convert.next)
3046                {
3047                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3048                   {
3049                      Type constType = null;
3050                      bool success = false;
3051                      // Conversion after = (conversions != null) ? conversions.last : null;
3052
3053                      if(!convert.dataType)
3054                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3055
3056                      if(warnConst && convert.dataType.kind == pointerType && convert.dataType.type && dest.constant)
3057                      {
3058                         Type ptrType { };
3059                         constType = { kind = pointerType, refCount = 1, type = ptrType };
3060                         CopyTypeInto(ptrType, convert.dataType.type);
3061                         ptrType.constant = true;
3062                      }
3063
3064                      // Just added this equality check to prevent recursion.... Make it safer?
3065                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3066                      if((constType || convert.dataType != dest) && MatchTypes(source, constType ? constType : convert.dataType, conversions, null, null, true, false /*true*/, false, true, warnConst))
3067                      {
3068                         if(!conversions && !convert.Set)
3069                            success = true;
3070                         else if(conversions != null)
3071                         {
3072                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3073                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3074                               (source.kind != classType || source._class.registered != _class.base))
3075                               success = true;
3076                            else
3077                            {
3078                               // *** Testing this! ***
3079                               Conversion conv { convert = convert };
3080                               conversions.Add(conv);
3081                               //conversions.Insert(after, conv);
3082                               success = true;
3083                            }
3084                         }
3085                      }
3086                      if(constType)
3087                         FreeType(constType);
3088                      if(success)
3089                         return true;
3090                   }
3091                }
3092             }
3093             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3094             {
3095                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3096                   (source.kind != classType || source._class.registered.type != structClass))
3097                   return true;
3098             }*/
3099
3100             // TESTING THIS... IS THIS OK??
3101             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3102             {
3103                if(!dest._class.registered.dataType)
3104                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3105                // Only support this for classes...
3106                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3107                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3108                {
3109                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, dest._class.registered.dataType.kind == classType, false, false, warnConst))
3110                   {
3111                      return true;
3112                   }
3113                }
3114             }
3115          }
3116
3117          // Moved this lower
3118          if(source.kind == classType)
3119          {
3120             Class _class;
3121             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3122             {
3123                Property convert;
3124                for(convert = _class.conversions.first; convert; convert = convert.next)
3125                {
3126                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3127                   {
3128                      Conversion after = (conversions != null) ? conversions.last : null;
3129
3130                      if(!convert.dataType)
3131                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3132                      if(convert.dataType != source &&
3133                         (!isConversionExploration || convert.dataType.kind == classType || !strcmp(_class.name, "String")) &&
3134                         MatchTypes(convert.dataType, dest, conversions, null, null, convert.dataType.kind == classType, convert.dataType.kind == classType, false, true, warnConst))
3135                      {
3136                         if(!conversions && !convert.Get)
3137                            return true;
3138                         else if(conversions != null)
3139                         {
3140                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3141                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3142                               (dest.kind != classType || dest._class.registered != _class.base))
3143                               return true;
3144                            else
3145                            {
3146                               Conversion conv { convert = convert, isGet = true };
3147
3148                               // conversions.Add(conv);
3149                               conversions.Insert(after, conv);
3150                               return true;
3151                            }
3152                         }
3153                      }
3154                   }
3155                }
3156             }
3157
3158             // TESTING THIS... IS THIS OK??
3159             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3160             {
3161                if(!source._class.registered.dataType)
3162                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3163                if(!isConversionExploration || source._class.registered.dataType.kind == classType || !strcmp(source._class.registered.name, "String"))
3164                {
3165                   if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, source._class.registered.dataType.kind == classType, source._class.registered.dataType.kind == classType, false, false, warnConst))
3166                      return true;
3167                   // For bool to be accepted by byte, short, etc.
3168                   else if(MatchTypes(dest, source._class.registered.dataType, null, null, null, false, false, false, false, warnConst))
3169                      return true;
3170                }
3171             }
3172          }
3173       }
3174
3175       if(source.kind == classType || source.kind == subClassType)
3176          ;
3177       else if(dest.kind == source.kind &&
3178          (dest.kind != structType && dest.kind != unionType &&
3179           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3180           return true;
3181       // RECENTLY ADDED THESE
3182       else if(dest.kind == doubleType && source.kind == floatType)
3183          return true;
3184       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3185          return true;
3186       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3187          return true;
3188       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3189          return true;
3190       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3191          return true;
3192       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3193          return true;
3194       else if(source.kind == enumType &&
3195          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3196           return true;
3197       else if(dest.kind == enumType && !isConversionExploration &&
3198          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3199           return true;
3200       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3201               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3202       {
3203          Type paramSource, paramDest;
3204
3205          if(dest.kind == methodType)
3206             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3207          if(source.kind == methodType)
3208             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3209
3210          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3211          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3212          if(dest.kind == methodType)
3213             dest = dest.method.dataType;
3214          if(source.kind == methodType)
3215             source = source.method.dataType;
3216
3217          paramSource = source.params.first;
3218          if(paramSource && paramSource.kind == voidType) paramSource = null;
3219          paramDest = dest.params.first;
3220          if(paramDest && paramDest.kind == voidType) paramDest = null;
3221
3222
3223          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3224             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3225          {
3226             // Source thisClass must be derived from destination thisClass
3227             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3228                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3229             {
3230                if(paramDest && paramDest.kind == classType)
3231                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3232                else
3233                   Compiler_Error($"method class should not take an object\n");
3234                return false;
3235             }
3236             paramDest = paramDest.next;
3237          }
3238          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3239          {
3240             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3241             {
3242                if(dest.thisClass)
3243                {
3244                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3245                   {
3246                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3247                      return false;
3248                   }
3249                }
3250                else
3251                {
3252                   // THIS WAS BACKWARDS:
3253                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3254                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3255                   {
3256                      if(owningClassDest)
3257                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3258                      else
3259                         Compiler_Error($"overriding class expected to be derived from method class\n");
3260                      return false;
3261                   }
3262                }
3263                paramSource = paramSource.next;
3264             }
3265             else
3266             {
3267                if(dest.thisClass)
3268                {
3269                   // Source thisClass must be derived from destination thisClass
3270                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3271                   {
3272                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3273                      return false;
3274                   }
3275                }
3276                else
3277                {
3278                   // THIS WAS BACKWARDS TOO??
3279                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3280                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3281                   {
3282                      //if(owningClass)
3283                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3284                      //else
3285                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3286                      return false;
3287                   }
3288                }
3289             }
3290          }
3291
3292
3293          // Source return type must be derived from destination return type
3294          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false, warnConst))
3295          {
3296             Compiler_Warning($"incompatible return type for function\n");
3297             return false;
3298          }
3299          // The const check is backwards from the MatchTypes above (for derivative classes checks)
3300          else
3301             CheckConstCompatibility(dest.returnType, source.returnType, true);
3302
3303          // Check parameters
3304
3305          for(; paramDest; paramDest = paramDest.next)
3306          {
3307             if(!paramSource)
3308             {
3309                //Compiler_Warning($"not enough parameters\n");
3310                Compiler_Error($"not enough parameters\n");
3311                return false;
3312             }
3313             {
3314                Type paramDestType = paramDest;
3315                Type paramSourceType = paramSource;
3316                Type type = paramDestType;
3317
3318                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3319                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3320                   paramSource.kind != templateType)
3321                {
3322                   int id = 0;
3323                   ClassTemplateParameter curParam = null;
3324                   Class sClass;
3325                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3326                   {
3327                      id = 0;
3328                      if(sClass.templateClass) sClass = sClass.templateClass;
3329                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3330                      {
3331                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3332                         {
3333                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3334                            {
3335                               if(sClass.templateClass) sClass = sClass.templateClass;
3336                               id += sClass.templateParams.count;
3337                            }
3338                            break;
3339                         }
3340                         id++;
3341                      }
3342                      if(curParam) break;
3343                   }
3344
3345                   if(curParam)
3346                   {
3347                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3348                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3349                   }
3350                }
3351
3352                // paramDest must be derived from paramSource
3353                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false, warnConst) &&
3354                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false, warnConst)))
3355                {
3356                   char type[1024];
3357                   type[0] = 0;
3358                   PrintType(paramDest, type, false, true);
3359                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3360
3361                   if(paramDestType != paramDest)
3362                      FreeType(paramDestType);
3363                   return false;
3364                }
3365                if(paramDestType != paramDest)
3366                   FreeType(paramDestType);
3367             }
3368
3369             paramSource = paramSource.next;
3370          }
3371          if(paramSource)
3372          {
3373             Compiler_Error($"too many parameters\n");
3374             return false;
3375          }
3376          return true;
3377       }
3378       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3379       {
3380          return true;
3381       }
3382       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3383          (source.kind == arrayType || source.kind == pointerType))
3384       {
3385          // Pointers to pointer is incompatible with non normal/nohead classes
3386          if(!(dest.type && dest.type.kind == pointerType && source.type.kind == classType && source.type._class &&
3387             source.type._class.registered && (source.type._class.registered.type != normalClass && source.type._class.registered.type != noHeadClass) && !source.type.byReference))
3388          {
3389             ComputeTypeSize(source.type);
3390             ComputeTypeSize(dest.type);
3391             if(source.type.size == dest.type.size && MatchTypes(source.type, dest.type, null, null, null, true, true, false, false, warnConst))
3392                return true;
3393          }
3394       }
3395    }
3396    return false;
3397 }
3398
3399 static void FreeConvert(Conversion convert)
3400 {
3401    if(convert.resultType)
3402       FreeType(convert.resultType);
3403 }
3404
3405 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3406                               char * string, OldList conversions)
3407 {
3408    BTNamedLink link;
3409
3410    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3411    {
3412       Class _class = link.data;
3413       if(_class.type == enumClass)
3414       {
3415          OldList converts { };
3416          Type type { };
3417          type.kind = classType;
3418
3419          if(!_class.symbol)
3420             _class.symbol = FindClass(_class.fullName);
3421          type._class = _class.symbol;
3422
3423          if(MatchTypes(type, dest, &converts, null, null, dest.kind != classType || !dest._class || strcmp(dest._class.string, "bool"),
3424                false, false, false, false))
3425          {
3426             NamedLink64 value;
3427             Class enumClass = eSystem_FindClass(privateModule, "enum");
3428             if(enumClass)
3429             {
3430                Class baseClass;
3431                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3432                {
3433                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3434                   for(value = e.values.first; value; value = value.next)
3435                   {
3436                      if(!strcmp(value.name, string))
3437                         break;
3438                   }
3439                   if(value)
3440                   {
3441                      FreeType(sourceExp.expType);
3442
3443                      sourceExp.isConstant = true;
3444                      sourceExp.expType = MkClassType(baseClass.fullName);
3445                      if(inCompiler || inPreCompiler || inDebugger)
3446                      {
3447                         char constant[256];
3448                         FreeExpContents(sourceExp);
3449
3450                         sourceExp.type = constantExp;
3451                         if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "short") || !strcmp(baseClass.dataTypeString, "char"))
3452                            sprintf(constant, FORMAT64D, value.data);
3453                         else
3454                            sprintf(constant, FORMAT64HEXLL, value.data);
3455                         sourceExp.constant = CopyString(constant);
3456                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3457                      }
3458
3459                      while(converts.first)
3460                      {
3461                         Conversion convert = converts.first;
3462                         converts.Remove(convert);
3463                         conversions.Add(convert);
3464                      }
3465                      delete type;
3466                      return true;
3467                   }
3468                }
3469             }
3470          }
3471          if(converts.first)
3472             converts.Free(FreeConvert);
3473          delete type;
3474       }
3475    }
3476    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3477       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3478          return true;
3479    return false;
3480 }
3481
3482 public bool ModuleVisibility(Module searchIn, Module searchFor)
3483 {
3484    SubModule subModule;
3485
3486    if(searchFor == searchIn)
3487       return true;
3488
3489    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3490    {
3491       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3492       {
3493          if(ModuleVisibility(subModule.module, searchFor))
3494             return true;
3495       }
3496    }
3497    return false;
3498 }
3499
3500 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3501 {
3502    Module module;
3503
3504    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3505       return true;
3506    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3507       return true;
3508    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3509       return true;
3510
3511    for(module = mainModule.application.allModules.first; module; module = module.next)
3512    {
3513       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3514          return true;
3515    }
3516    return false;
3517 }
3518
3519 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla, bool warnConst)
3520 {
3521    Type source;
3522    Type realDest = dest;
3523    Type backupSourceExpType = null;
3524    Expression nbExp = GetNonBracketsExp(sourceExp);
3525    Expression computedExp = nbExp;
3526    dest.refCount++;
3527
3528    if(sourceExp.isConstant && sourceExp.type != constantExp && sourceExp.type != identifierExp && sourceExp.type != castExp &&
3529       dest.kind == classType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3530    {
3531       computedExp = CopyExpression(nbExp);        // Keep the original expression, but compute for checking enum ranges
3532       ComputeExpression(computedExp /*sourceExp*/);
3533    }
3534
3535    source = sourceExp.expType;
3536
3537    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3538    {
3539       if(computedExp != nbExp)
3540       {
3541          FreeExpression(computedExp);
3542          computedExp = nbExp;
3543       }
3544       FreeType(dest);
3545       return true;
3546    }
3547
3548    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3549    {
3550        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3551        {
3552           Class sourceBase, destBase;
3553           for(sourceBase = source._class.registered;
3554               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3555               sourceBase = sourceBase.base);
3556           for(destBase = dest._class.registered;
3557               destBase && destBase.base && destBase.base.type != systemClass;
3558               destBase = destBase.base);
3559           //if(source._class.registered == dest._class.registered)
3560           if(sourceBase == destBase)
3561           {
3562             if(computedExp != nbExp)
3563             {
3564                FreeExpression(computedExp);
3565                computedExp = nbExp;
3566             }
3567             FreeType(dest);
3568             return true;
3569          }
3570       }
3571    }
3572
3573    if(source)
3574    {
3575       OldList * specs;
3576       bool flag = false;
3577       int64 value = MAXINT;
3578
3579       source.refCount++;
3580
3581       if(computedExp.type == constantExp)
3582       {
3583          if(source.isSigned)
3584             value = strtoll(computedExp.constant, null, 0);
3585          else
3586             value = strtoull(computedExp.constant, null, 0);
3587       }
3588       else if(computedExp.type == opExp && sourceExp.op.op == '-' && !computedExp.op.exp1 && computedExp.op.exp2 && computedExp.op.exp2.type == constantExp)
3589       {
3590          if(source.isSigned)
3591             value = -strtoll(computedExp.op.exp2.constant, null, 0);
3592          else
3593             value = -strtoull(computedExp.op.exp2.constant, null, 0);
3594       }
3595       if(computedExp != nbExp)
3596       {
3597          FreeExpression(computedExp);
3598          computedExp = nbExp;
3599       }
3600
3601       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3602          !strcmp(source._class.registered.fullName, "unichar" /*"ecere::com::unichar"*/))
3603       {
3604          FreeType(source);
3605          source = Type { kind = intType, isSigned = false, refCount = 1 };
3606       }
3607
3608       if(dest.kind == classType)
3609       {
3610          Class _class = dest._class ? dest._class.registered : null;
3611
3612          if(_class && _class.type == unitClass)
3613          {
3614             if(source.kind != classType)
3615             {
3616                Type tempType { };
3617                Type tempDest, tempSource;
3618
3619                for(; _class.base.type != systemClass; _class = _class.base);
3620                tempSource = dest;
3621                tempDest = tempType;
3622
3623                tempType.kind = classType;
3624                if(!_class.symbol)
3625                   _class.symbol = FindClass(_class.fullName);
3626
3627                tempType._class = _class.symbol;
3628                tempType.truth = dest.truth;
3629                if(tempType._class)
3630                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3631
3632                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3633                backupSourceExpType = sourceExp.expType;
3634                if(dest.passAsTemplate)
3635                {
3636                   // Don't carry passAsTemplate
3637                   sourceExp.expType = { };
3638                   CopyTypeInto(sourceExp.expType, dest);
3639                   sourceExp.expType.passAsTemplate = false;
3640                }
3641                else
3642                {
3643                   sourceExp.expType = dest;
3644                   dest.refCount++;
3645                }
3646                //sourceExp.expType = MkClassType(_class.fullName);
3647                flag = true;
3648
3649                delete tempType;
3650             }
3651          }
3652
3653
3654          // Why wasn't there something like this?
3655          if(_class && _class.type == bitClass && source.kind != classType)
3656          {
3657             if(!dest._class.registered.dataType)
3658                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3659             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false, warnConst))
3660             {
3661                FreeType(source);
3662                FreeType(sourceExp.expType);
3663                source = sourceExp.expType = MkClassType(dest._class.string);
3664                source.refCount++;
3665
3666                //source.kind = classType;
3667                //source._class = dest._class;
3668             }
3669          }
3670
3671          // Adding two enumerations
3672          /*
3673          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3674          {
3675             if(!source._class.registered.dataType)
3676                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3677             if(!dest._class.registered.dataType)
3678                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3679
3680             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3681             {
3682                FreeType(source);
3683                source = sourceExp.expType = MkClassType(dest._class.string);
3684                source.refCount++;
3685
3686                //source.kind = classType;
3687                //source._class = dest._class;
3688             }
3689          }*/
3690
3691          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3692          {
3693             OldList * specs = MkList();
3694             Declarator decl;
3695             char string[1024];
3696
3697             ReadString(string, sourceExp.string);
3698             decl = SpecDeclFromString(string, specs, null);
3699
3700             FreeExpContents(sourceExp);
3701             FreeType(sourceExp.expType);
3702
3703             sourceExp.type = classExp;
3704             sourceExp._classExp.specifiers = specs;
3705             sourceExp._classExp.decl = decl;
3706             sourceExp.expType = dest;
3707             dest.refCount++;
3708
3709             FreeType(source);
3710             FreeType(dest);
3711             if(backupSourceExpType) FreeType(backupSourceExpType);
3712             return true;
3713          }
3714       }
3715       else if(source.kind == classType)
3716       {
3717          Class _class = source._class ? source._class.registered : null;
3718
3719          if(_class && (_class.type == unitClass || /*!strcmp(_class.fullName, "bool") || _class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3720          {
3721             /*
3722             if(dest.kind != classType)
3723             {
3724                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3725                if(!source._class.registered.dataType)
3726                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3727
3728                FreeType(dest);
3729                dest = MkClassType(source._class.string);
3730                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3731                //   dest = MkClassType(source._class.string);
3732             }
3733             */
3734
3735             if(dest.kind != classType)
3736             {
3737                Type tempType { };
3738                Type tempDest, tempSource;
3739
3740                if(!source._class.registered.dataType)
3741                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3742
3743                for(; _class.base.type != systemClass; _class = _class.base);
3744                tempDest = source;
3745                tempSource = tempType;
3746                tempType.kind = classType;
3747                tempType._class = FindClass(_class.fullName);
3748                tempType.truth = source.truth;
3749                tempType.classObjectType = source.classObjectType;
3750
3751                if(tempType._class)
3752                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false, warnConst);
3753
3754                // PUT THIS BACK TESTING UNITS?
3755                if(conversions && conversions.last)
3756                {
3757                   ((Conversion)(conversions.last)).resultType = dest;
3758                   dest.refCount++;
3759                }
3760
3761                FreeType(sourceExp.expType);
3762                sourceExp.expType = MkClassType(_class.fullName);
3763                sourceExp.expType.truth = source.truth;
3764                sourceExp.expType.classObjectType = source.classObjectType;
3765
3766                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3767
3768                if(!sourceExp.destType)
3769                {
3770                   FreeType(sourceExp.destType);
3771                   sourceExp.destType = sourceExp.expType;
3772                   if(sourceExp.expType)
3773                      sourceExp.expType.refCount++;
3774                }
3775                //flag = true;
3776                //source = _class.dataType;
3777
3778
3779                // TOCHECK: TESTING THIS NEW CODE
3780                if(!_class.dataType)
3781                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3782                FreeType(dest);
3783                dest = MkClassType(source._class.string);
3784                dest.truth = source.truth;
3785                dest.classObjectType = source.classObjectType;
3786
3787                FreeType(source);
3788                source = _class.dataType;
3789                source.refCount++;
3790
3791                delete tempType;
3792             }
3793          }
3794       }
3795
3796       if(!flag)
3797       {
3798          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false, warnConst))
3799          {
3800             FreeType(source);
3801             FreeType(dest);
3802             return true;
3803          }
3804       }
3805
3806       // Implicit Casts
3807       /*
3808       if(source.kind == classType)
3809       {
3810          Class _class = source._class.registered;
3811          if(_class.type == unitClass)
3812          {
3813             if(!_class.dataType)
3814                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3815             source = _class.dataType;
3816          }
3817       }*/
3818
3819       if(dest.kind == classType)
3820       {
3821          Class _class = dest._class ? dest._class.registered : null;
3822          bool fittingValue = false;
3823          if(_class && _class.type == enumClass)
3824          {
3825             Class enumClass = eSystem_FindClass(privateModule, "enum");
3826             EnumClassData c = ACCESS_CLASSDATA(_class, enumClass);
3827             if(c && value >= 0 && value <= c.largest)
3828                fittingValue = true;
3829          }
3830
3831          if(_class && !dest.truth && (_class.type == unitClass || fittingValue ||
3832             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3833          {
3834             if(_class.type == normalClass || _class.type == noHeadClass)
3835             {
3836                Expression newExp { };
3837                *newExp = *sourceExp;
3838                if(sourceExp.destType) sourceExp.destType.refCount++;
3839                if(sourceExp.expType)  sourceExp.expType.refCount++;
3840                sourceExp.type = castExp;
3841                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3842                sourceExp.cast.exp = newExp;
3843                FreeType(sourceExp.expType);
3844                sourceExp.expType = null;
3845                ProcessExpressionType(sourceExp);
3846
3847                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3848                if(!inCompiler)
3849                {
3850                   FreeType(sourceExp.expType);
3851                   sourceExp.expType = dest;
3852                }
3853
3854                FreeType(source);
3855                if(inCompiler) FreeType(dest);
3856
3857                if(backupSourceExpType) FreeType(backupSourceExpType);
3858                return true;
3859             }
3860
3861             if(!_class.dataType)
3862                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3863             FreeType(dest);
3864             dest = _class.dataType;
3865             dest.refCount++;
3866          }
3867
3868          // Accept lower precision types for units, since we want to keep the unit type
3869          if(dest.kind == doubleType &&
3870             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3871              source.kind == charType || source.kind == _BoolType))
3872          {
3873             specs = MkListOne(MkSpecifier(DOUBLE));
3874          }
3875          else if(dest.kind == floatType &&
3876             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3877             source.kind == _BoolType || source.kind == doubleType))
3878          {
3879             specs = MkListOne(MkSpecifier(FLOAT));
3880          }
3881          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3882             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3883          {
3884             specs = MkList();
3885             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3886             ListAdd(specs, MkSpecifier(INT64));
3887          }
3888          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3889             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3890          {
3891             specs = MkList();
3892             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3893             ListAdd(specs, MkSpecifier(INT));
3894          }
3895          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3896             source.kind == floatType || source.kind == doubleType))
3897          {
3898             specs = MkList();
3899             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3900             ListAdd(specs, MkSpecifier(SHORT));
3901          }
3902          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3903             source.kind == floatType || source.kind == doubleType))
3904          {
3905             specs = MkList();
3906             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3907             ListAdd(specs, MkSpecifier(CHAR));
3908          }
3909          else
3910          {
3911             FreeType(source);
3912             FreeType(dest);
3913             if(backupSourceExpType)
3914             {
3915                // Failed to convert: revert previous exp type
3916                if(sourceExp.expType) FreeType(sourceExp.expType);
3917                sourceExp.expType = backupSourceExpType;
3918             }
3919             return false;
3920          }
3921       }
3922       else if(dest.kind == doubleType &&
3923          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3924           source.kind == _BoolType || source.kind == charType))
3925       {
3926          specs = MkListOne(MkSpecifier(DOUBLE));
3927       }
3928       else if(dest.kind == floatType &&
3929          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3930       {
3931          specs = MkListOne(MkSpecifier(FLOAT));
3932       }
3933       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3934          (value == 1 || value == 0))
3935       {
3936          specs = MkList();
3937          ListAdd(specs, MkSpecifier(BOOL));
3938       }
3939       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3940          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3941       {
3942          if(source.kind == intType)
3943          {
3944             FreeType(dest);
3945             FreeType(source);
3946             if(backupSourceExpType) FreeType(backupSourceExpType);
3947             return true;
3948          }
3949          else
3950          {
3951             specs = MkList();
3952             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3953             ListAdd(specs, MkSpecifier(CHAR));
3954          }
3955       }
3956       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3957          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3958       {
3959          if(source.kind == intType)
3960          {
3961             FreeType(dest);
3962             FreeType(source);
3963             if(backupSourceExpType) FreeType(backupSourceExpType);
3964             return true;
3965          }
3966          else
3967          {
3968             specs = MkList();
3969             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3970             ListAdd(specs, MkSpecifier(SHORT));
3971          }
3972       }
3973       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3974       {
3975          specs = MkList();
3976          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3977          ListAdd(specs, MkSpecifier(INT));
3978       }
3979       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3980       {
3981          specs = MkList();
3982          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3983          ListAdd(specs, MkSpecifier(INT64));
3984       }
3985       else if(dest.kind == enumType &&
3986          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3987       {
3988          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3989       }
3990       else
3991       {
3992          FreeType(source);
3993          FreeType(dest);
3994          if(backupSourceExpType)
3995          {
3996             // Failed to convert: revert previous exp type
3997             if(sourceExp.expType) FreeType(sourceExp.expType);
3998             sourceExp.expType = backupSourceExpType;
3999          }
4000          return false;
4001       }
4002
4003       if(!flag && !sourceExp.opDestType)
4004       {
4005          Expression newExp { };
4006          *newExp = *sourceExp;
4007          newExp.prev = null;
4008          newExp.next = null;
4009          if(sourceExp.destType) sourceExp.destType.refCount++;
4010          if(sourceExp.expType)  sourceExp.expType.refCount++;
4011
4012          sourceExp.type = castExp;
4013          if(realDest.kind == classType)
4014          {
4015             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
4016             FreeList(specs, FreeSpecifier);
4017          }
4018          else
4019             sourceExp.cast.typeName = MkTypeName(specs, null);
4020          if(newExp.type == opExp)
4021          {
4022             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
4023          }
4024          else
4025             sourceExp.cast.exp = newExp;
4026
4027          FreeType(sourceExp.expType);
4028          sourceExp.expType = null;
4029          ProcessExpressionType(sourceExp);
4030       }
4031       else
4032          FreeList(specs, FreeSpecifier);
4033
4034       FreeType(dest);
4035       FreeType(source);
4036       if(backupSourceExpType) FreeType(backupSourceExpType);
4037
4038       return true;
4039    }
4040    else
4041    {
4042       if(computedExp != nbExp)
4043       {
4044          FreeExpression(computedExp);
4045          computedExp = nbExp;
4046       }
4047
4048       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
4049       if(sourceExp.type == identifierExp)
4050       {
4051          Identifier id = sourceExp.identifier;
4052          if(dest.kind == classType)
4053          {
4054             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
4055             {
4056                Class _class = dest._class.registered;
4057                Class enumClass = eSystem_FindClass(privateModule, "enum");
4058                if(enumClass)
4059                {
4060                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
4061                   {
4062                      NamedLink64 value;
4063                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4064                      for(value = e.values.first; value; value = value.next)
4065                      {
4066                         if(!strcmp(value.name, id.string))
4067                            break;
4068                      }
4069                      if(value)
4070                      {
4071                         FreeType(sourceExp.expType);
4072
4073                         sourceExp.isConstant = true;
4074                         sourceExp.expType = MkClassType(_class.fullName);
4075                         if(inCompiler || inPreCompiler || inDebugger)
4076                         {
4077                            FreeExpContents(sourceExp);
4078
4079                            sourceExp.type = constantExp;
4080                            if(_class.dataTypeString && (!strcmp(_class.dataTypeString, "int") || !strcmp(_class.dataTypeString, "int64") || !strcmp(_class.dataTypeString, "short") || !strcmp(_class.dataTypeString, "char"))) // _class cannot be null here!
4081                               sourceExp.constant = PrintInt64(value.data);
4082                            else
4083                               sourceExp.constant = PrintUInt64(value.data);
4084                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
4085                         }
4086                         FreeType(dest);
4087                         return true;
4088                      }
4089                   }
4090                }
4091             }
4092          }
4093
4094          // Loop through all enum classes
4095          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
4096          {
4097             FreeType(dest);
4098             return true;
4099          }
4100       }
4101       FreeType(dest);
4102    }
4103    return false;
4104 }
4105
4106 #define TERTIARY(o, name, m, t, p) \
4107    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4108    {                                                              \
4109       exp.type = constantExp;                                    \
4110       exp.string = p(op1.m ? op2.m : op3.m);                     \
4111       if(!exp.expType) \
4112          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4113       return true;                                                \
4114    }
4115
4116 #define BINARY(o, name, m, t, p) \
4117    static bool name(Expression exp, Operand op1, Operand op2)   \
4118    {                                                              \
4119       t value2 = op2.m;                                           \
4120       exp.type = constantExp;                                    \
4121       exp.string = p((t)(op1.m o value2));                     \
4122       if(!exp.expType) \
4123          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4124       return true;                                                \
4125    }
4126
4127 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4128    static bool name(Expression exp, Operand op1, Operand op2)   \
4129    {                                                              \
4130       t value2 = op2.m;                                           \
4131       exp.type = constantExp;                                    \
4132       exp.string = p(value2 ? ((t)(op1.m o value2)) : 0);             \
4133       if(!exp.expType) \
4134          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4135       return true;                                                \
4136    }
4137
4138 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4139    static bool name(Expression exp, Operand op1, Operand op2)   \
4140    {                                                              \
4141       t value2 = op2.m;                                           \
4142       exp.type = constantExp;                                    \
4143       exp.string = p((t)(op1.m o value2));             \
4144       if(!exp.expType) \
4145          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4146       return true;                                                \
4147    }
4148
4149 #define UNARY(o, name, m, t, p) \
4150    static bool name(Expression exp, Operand op1)                \
4151    {                                                              \
4152       exp.type = constantExp;                                    \
4153       exp.string = p((t)(o op1.m));                                   \
4154       if(!exp.expType) \
4155          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4156       return true;                                                \
4157    }
4158
4159 #define OPERATOR_ALL(macro, o, name) \
4160    macro(o, Int##name, i, int, PrintInt) \
4161    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4162    macro(o, Int64##name, i64, int64, PrintInt64) \
4163    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4164    macro(o, Short##name, s, short, PrintShort) \
4165    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4166    macro(o, Char##name, c, char, PrintChar) \
4167    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4168    macro(o, Float##name, f, float, PrintFloat) \
4169    macro(o, Double##name, d, double, PrintDouble)
4170
4171 #define OPERATOR_INTTYPES(macro, o, name) \
4172    macro(o, Int##name, i, int, PrintInt) \
4173    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4174    macro(o, Int64##name, i64, int64, PrintInt64) \
4175    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4176    macro(o, Short##name, s, short, PrintShort) \
4177    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4178    macro(o, Char##name, c, char, PrintChar) \
4179    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4180
4181 #define OPERATOR_REALTYPES(macro, o, name) \
4182    macro(o, Float##name, f, float, PrintFloat) \
4183    macro(o, Double##name, d, double, PrintDouble)
4184
4185 // binary arithmetic
4186 OPERATOR_ALL(BINARY, +, Add)
4187 OPERATOR_ALL(BINARY, -, Sub)
4188 OPERATOR_ALL(BINARY, *, Mul)
4189 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4190 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4191 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4192
4193 // unary arithmetic
4194 OPERATOR_ALL(UNARY, -, Neg)
4195
4196 // unary arithmetic increment and decrement
4197 OPERATOR_ALL(UNARY, ++, Inc)
4198 OPERATOR_ALL(UNARY, --, Dec)
4199
4200 // binary arithmetic assignment
4201 OPERATOR_ALL(BINARY, =, Asign)
4202 OPERATOR_ALL(BINARY, +=, AddAsign)
4203 OPERATOR_ALL(BINARY, -=, SubAsign)
4204 OPERATOR_ALL(BINARY, *=, MulAsign)
4205 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4206 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4207 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4208
4209 // binary bitwise
4210 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4211 OPERATOR_INTTYPES(BINARY, |, BitOr)
4212 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4213 OPERATOR_INTTYPES(BINARY, <<, LShift)
4214 OPERATOR_INTTYPES(BINARY, >>, RShift)
4215
4216 // unary bitwise
4217 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4218
4219 // binary bitwise assignment
4220 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4221 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4222 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4223 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4224 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4225
4226 // unary logical negation
4227 OPERATOR_INTTYPES(UNARY, !, Not)
4228
4229 // binary logical equality
4230 OPERATOR_ALL(BINARY, ==, Equ)
4231 OPERATOR_ALL(BINARY, !=, Nqu)
4232
4233 // binary logical
4234 OPERATOR_ALL(BINARY, &&, And)
4235 OPERATOR_ALL(BINARY, ||, Or)
4236
4237 // binary logical relational
4238 OPERATOR_ALL(BINARY, >, Grt)
4239 OPERATOR_ALL(BINARY, <, Sma)
4240 OPERATOR_ALL(BINARY, >=, GrtEqu)
4241 OPERATOR_ALL(BINARY, <=, SmaEqu)
4242
4243 // tertiary condition operator
4244 OPERATOR_INTTYPES(TERTIARY, ?, Cond)
4245
4246 //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
4247 #define OPERATOR_TABLE_ALL(name, type) \
4248     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4249                           type##Neg, \
4250                           type##Inc, type##Dec, \
4251                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4252                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4253                           type##BitNot, \
4254                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4255                           type##Not, \
4256                           type##Equ, type##Nqu, \
4257                           type##And, type##Or, \
4258                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4259                         }; \
4260
4261 #define OPERATOR_TABLE_INTTYPES(name, type) \
4262     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4263                           type##Neg, \
4264                           type##Inc, type##Dec, \
4265                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4266                           null, null, null, null, null, \
4267                           null, \
4268                           null, null, null, null, null, \
4269                           null, \
4270                           type##Equ, type##Nqu, \
4271                           type##And, type##Or, \
4272                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4273                         }; \
4274
4275 OPERATOR_TABLE_ALL(int, Int)
4276 OPERATOR_TABLE_ALL(uint, UInt)
4277 OPERATOR_TABLE_ALL(int64, Int64)
4278 OPERATOR_TABLE_ALL(uint64, UInt64)
4279 OPERATOR_TABLE_ALL(short, Short)
4280 OPERATOR_TABLE_ALL(ushort, UShort)
4281 OPERATOR_TABLE_INTTYPES(float, Float)
4282 OPERATOR_TABLE_INTTYPES(double, Double)
4283 OPERATOR_TABLE_ALL(char, Char)
4284 OPERATOR_TABLE_ALL(uchar, UChar)
4285
4286 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4287 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4288 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4289 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4290 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4291 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4292 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4293 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4294
4295 public void ReadString(char * output,  char * string)
4296 {
4297    int len = strlen(string);
4298    int c,d = 0;
4299    bool quoted = false, escaped = false;
4300    for(c = 0; c<len; c++)
4301    {
4302       char ch = string[c];
4303       if(escaped)
4304       {
4305          switch(ch)
4306          {
4307             case 'n': output[d] = '\n'; break;
4308             case 't': output[d] = '\t'; break;
4309             case 'a': output[d] = '\a'; break;
4310             case 'b': output[d] = '\b'; break;
4311             case 'f': output[d] = '\f'; break;
4312             case 'r': output[d] = '\r'; break;
4313             case 'v': output[d] = '\v'; break;
4314             case '\\': output[d] = '\\'; break;
4315             case '\"': output[d] = '\"'; break;
4316             case '\'': output[d] = '\''; break;
4317             default: output[d] = ch;
4318          }
4319          d++;
4320          escaped = false;
4321       }
4322       else
4323       {
4324          if(ch == '\"')
4325             quoted ^= true;
4326          else if(quoted)
4327          {
4328             if(ch == '\\')
4329                escaped = true;
4330             else
4331                output[d++] = ch;
4332          }
4333       }
4334    }
4335    output[d] = '\0';
4336 }
4337
4338 // String Unescape Copy
4339
4340 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4341 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4342 public int UnescapeString(char * d, char * s, int len)
4343 {
4344    int j = 0, k = 0;
4345    char ch;
4346    while(j < len && (ch = s[j]))
4347    {
4348       switch(ch)
4349       {
4350          case '\\':
4351             switch((ch = s[++j]))
4352             {
4353                case 'n': d[k] = '\n'; break;
4354                case 't': d[k] = '\t'; break;
4355                case 'a': d[k] = '\a'; break;
4356                case 'b': d[k] = '\b'; break;
4357                case 'f': d[k] = '\f'; break;
4358                case 'r': d[k] = '\r'; break;
4359                case 'v': d[k] = '\v'; break;
4360                case '\\': d[k] = '\\'; break;
4361                case '\"': d[k] = '\"'; break;
4362                case '\'': d[k] = '\''; break;
4363                default: d[k] = '\\'; d[k] = ch;
4364             }
4365             break;
4366          default:
4367             d[k] = ch;
4368       }
4369       j++, k++;
4370    }
4371    d[k] = '\0';
4372    return k;
4373 }
4374
4375 public char * OffsetEscapedString(char * s, int len, int offset)
4376 {
4377    char ch;
4378    int j = 0, k = 0;
4379    while(j < len && k < offset && (ch = s[j]))
4380    {
4381       if(ch == '\\') ++j;
4382       j++, k++;
4383    }
4384    return (k == offset) ? s + j : null;
4385 }
4386
4387 public Operand GetOperand(Expression exp)
4388 {
4389    Operand op { };
4390    Type type = exp.expType;
4391    if(type)
4392    {
4393       while(type.kind == classType && type._class &&
4394          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4395       {
4396          if(!type._class.registered.dataType)
4397             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4398          type = type._class.registered.dataType;
4399
4400       }
4401       if(exp.type == stringExp && op.kind == pointerType)
4402       {
4403          op.ui64 = (uint64)(uintptr)exp.string;
4404          op.kind = pointerType;
4405          op.ops = uint64Ops;
4406       }
4407       else if(exp.isConstant && exp.type == constantExp)
4408       {
4409          op.kind = type.kind;
4410          op.type = type;
4411
4412          switch(op.kind)
4413          {
4414             case _BoolType:
4415             case charType:
4416             {
4417                if(exp.constant[0] == '\'')
4418                {
4419                   op.c = exp.constant[1];
4420                   op.ops = charOps;
4421                }
4422                else if(type.isSigned)
4423                {
4424                   op.c = (char)strtol(exp.constant, null, 0);
4425                   op.ops = charOps;
4426                }
4427                else
4428                {
4429                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4430                   op.ops = ucharOps;
4431                }
4432                break;
4433             }
4434             case shortType:
4435                if(exp.constant[0] == '\'')
4436                {
4437                   op.s = exp.constant[1];
4438                   op.ops = shortOps;
4439                }
4440                else if(type.isSigned)
4441                {
4442                   op.s = (short)strtol(exp.constant, null, 0);
4443                   op.ops = shortOps;
4444                }
4445                else
4446                {
4447                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4448                   op.ops = ushortOps;
4449                }
4450                break;
4451             case intType:
4452             case longType:
4453                if(exp.constant[0] == '\'')
4454                {
4455                   op.i = exp.constant[1];
4456                   op.ops = intOps;
4457                }
4458                else if(type.isSigned)
4459                {
4460                   op.i = (int)strtol(exp.constant, null, 0);
4461                   op.ops = intOps;
4462                }
4463                else
4464                {
4465                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4466                   op.ops = uintOps;
4467                }
4468                op.kind = intType;
4469                break;
4470             case int64Type:
4471                if(type.isSigned)
4472                {
4473                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4474                   op.ops = int64Ops;
4475                }
4476                else
4477                {
4478                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4479                   op.ops = uint64Ops;
4480                }
4481                op.kind = int64Type;
4482                break;
4483             case intPtrType:
4484                if(type.isSigned)
4485                {
4486                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4487                   op.ops = int64Ops;
4488                }
4489                else
4490                {
4491                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4492                   op.ops = uint64Ops;
4493                }
4494                op.kind = int64Type;
4495                break;
4496             case intSizeType:
4497                if(type.isSigned)
4498                {
4499                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4500                   op.ops = int64Ops;
4501                }
4502                else
4503                {
4504                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4505                   op.ops = uint64Ops;
4506                }
4507                op.kind = int64Type;
4508                break;
4509             case floatType:
4510                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4511                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4512                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4513                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4514                else
4515                   op.f = (float)strtod(exp.constant, null);
4516                op.ops = floatOps;
4517                break;
4518             case doubleType:
4519                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4520                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4521                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4522                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4523                else
4524                   op.d = (double)strtod(exp.constant, null);
4525                op.ops = doubleOps;
4526                break;
4527             //case classType:    For when we have operator overloading...
4528             // Pointer additions
4529             //case functionType:
4530             case arrayType:
4531             case pointerType:
4532             case classType:
4533                op.ui64 = _strtoui64(exp.constant, null, 0);
4534                op.kind = pointerType;
4535                op.ops = uint64Ops;
4536                // op.ptrSize =
4537                break;
4538          }
4539       }
4540    }
4541    return op;
4542 }
4543
4544 static int64 GetEnumValue(Class _class, void * ptr)
4545 {
4546    int64 v = 0;
4547    switch(_class.typeSize)
4548    {
4549       case 8:
4550          if(!strcmp(_class.dataTypeString, "uint64"))
4551             v = (int64)*(uint64 *)ptr;
4552          else
4553             v = (int64)*(int64 *)ptr;
4554          break;
4555       case 4:
4556          if(!strcmp(_class.dataTypeString, "uint"))
4557             v = (int64)*(uint *)ptr;
4558          else
4559             v = (int64)*(int *)ptr;
4560          break;
4561       case 2:
4562          if(!strcmp(_class.dataTypeString, "uint16"))
4563             v = (int64)*(uint16 *)ptr;
4564          else
4565             v = (int64)*(short *)ptr;
4566          break;
4567       case 1:
4568          if(!strcmp(_class.dataTypeString, "byte"))
4569             v = (int64)*(byte *)ptr;
4570          else
4571             v = (int64)*(char *)ptr;
4572          break;
4573    }
4574    return v;
4575 }
4576
4577 static __attribute__((unused)) void UnusedFunction()
4578 {
4579    int a;
4580    a.OnGetString(0,0,0);
4581 }
4582 default:
4583 extern int __ecereVMethodID_class_OnGetString;
4584 public:
4585
4586 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4587 {
4588    DataMember dataMember;
4589    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4590    {
4591       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4592          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4593       else
4594       {
4595          Expression exp { };
4596          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4597          Type type;
4598          void * ptr = inst.data + dataMember.offset + offset;
4599          char * result = null;
4600          exp.loc = member.loc = inst.loc;
4601          ((Identifier)member.identifiers->first).loc = inst.loc;
4602
4603          if(!dataMember.dataType)
4604             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4605          type = dataMember.dataType;
4606          if(type.kind == classType)
4607          {
4608             Class _class = type._class.registered;
4609             if(_class.type == enumClass)
4610             {
4611                Class enumClass = eSystem_FindClass(privateModule, "enum");
4612                if(enumClass)
4613                {
4614                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4615                   NamedLink64 item;
4616                   for(item = e.values.first; item; item = item.next)
4617                   {
4618                      if(item.data == GetEnumValue(_class, ptr))
4619                      {
4620                         result = item.name;
4621                         break;
4622                      }
4623                   }
4624                   if(result)
4625                   {
4626                      exp.identifier = MkIdentifier(result);
4627                      exp.type = identifierExp;
4628                      exp.destType = MkClassType(_class.fullName);
4629                      ProcessExpressionType(exp);
4630                   }
4631                }
4632             }
4633             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4634             {
4635                if(!_class.dataType)
4636                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4637                type = _class.dataType;
4638             }
4639          }
4640          if(!result)
4641          {
4642             switch(type.kind)
4643             {
4644                case floatType:
4645                {
4646                   FreeExpContents(exp);
4647
4648                   exp.constant = PrintFloat(*(float*)ptr);
4649                   exp.type = constantExp;
4650                   break;
4651                }
4652                case doubleType:
4653                {
4654                   FreeExpContents(exp);
4655
4656                   exp.constant = PrintDouble(*(double*)ptr);
4657                   exp.type = constantExp;
4658                   break;
4659                }
4660                case intType:
4661                {
4662                   FreeExpContents(exp);
4663
4664                   exp.constant = PrintInt(*(int*)ptr);
4665                   exp.type = constantExp;
4666                   break;
4667                }
4668                case int64Type:
4669                {
4670                   FreeExpContents(exp);
4671
4672                   exp.constant = PrintInt64(*(int64*)ptr);
4673                   exp.type = constantExp;
4674                   break;
4675                }
4676                case intPtrType:
4677                {
4678                   FreeExpContents(exp);
4679                   // TODO: This should probably use proper type
4680                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4681                   exp.type = constantExp;
4682                   break;
4683                }
4684                case intSizeType:
4685                {
4686                   FreeExpContents(exp);
4687                   // TODO: This should probably use proper type
4688                   exp.constant = PrintInt64((int64)*(intsize*)ptr);
4689                   exp.type = constantExp;
4690                   break;
4691                }
4692                default:
4693                   Compiler_Error($"Unhandled type populating instance\n");
4694             }
4695          }
4696          ListAdd(memberList, member);
4697       }
4698
4699       if(parentDataMember.type == unionMember)
4700          break;
4701    }
4702 }
4703
4704 void PopulateInstance(Instantiation inst)
4705 {
4706    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4707    Class _class = classSym.registered;
4708    DataMember dataMember;
4709    OldList * memberList = MkList();
4710    // Added this check and ->Add to prevent memory leaks on bad code
4711    if(!inst.members)
4712       inst.members = MkListOne(MkMembersInitList(memberList));
4713    else
4714       inst.members->Add(MkMembersInitList(memberList));
4715    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4716    {
4717       if(!dataMember.isProperty)
4718       {
4719          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4720             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4721          else
4722          {
4723             Expression exp { };
4724             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4725             Type type;
4726             void * ptr = inst.data + dataMember.offset;
4727             char * result = null;
4728
4729             exp.loc = member.loc = inst.loc;
4730             ((Identifier)member.identifiers->first).loc = inst.loc;
4731
4732             if(!dataMember.dataType)
4733                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4734             type = dataMember.dataType;
4735             if(type.kind == classType)
4736             {
4737                Class _class = type._class.registered;
4738                if(_class.type == enumClass)
4739                {
4740                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4741                   if(enumClass)
4742                   {
4743                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4744                      NamedLink64 item;
4745                      for(item = e.values.first; item; item = item.next)
4746                      {
4747                         if(item.data == GetEnumValue(_class, ptr))
4748                         {
4749                            result = item.name;
4750                            break;
4751                         }
4752                      }
4753                   }
4754                   if(result)
4755                   {
4756                      exp.identifier = MkIdentifier(result);
4757                      exp.type = identifierExp;
4758                      exp.destType = MkClassType(_class.fullName);
4759                      ProcessExpressionType(exp);
4760                   }
4761                }
4762                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4763                {
4764                   if(!_class.dataType)
4765                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4766                   type = _class.dataType;
4767                }
4768             }
4769             if(!result)
4770             {
4771                switch(type.kind)
4772                {
4773                   case floatType:
4774                   {
4775                      exp.constant = PrintFloat(*(float*)ptr);
4776                      exp.type = constantExp;
4777                      break;
4778                   }
4779                   case doubleType:
4780                   {
4781                      exp.constant = PrintDouble(*(double*)ptr);
4782                      exp.type = constantExp;
4783                      break;
4784                   }
4785                   case intType:
4786                   {
4787                      exp.constant = PrintInt(*(int*)ptr);
4788                      exp.type = constantExp;
4789                      break;
4790                   }
4791                   case int64Type:
4792                   {
4793                      exp.constant = PrintInt64(*(int64*)ptr);
4794                      exp.type = constantExp;
4795                      break;
4796                   }
4797                   case intPtrType:
4798                   {
4799                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4800                      exp.type = constantExp;
4801                      break;
4802                   }
4803                   default:
4804                      Compiler_Error($"Unhandled type populating instance\n");
4805                }
4806             }
4807             ListAdd(memberList, member);
4808          }
4809       }
4810    }
4811 }
4812
4813 void ComputeInstantiation(Expression exp)
4814 {
4815    Instantiation inst = exp.instance;
4816    MembersInit members;
4817    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4818    Class _class = classSym ? classSym.registered : null;
4819    DataMember curMember = null;
4820    Class curClass = null;
4821    DataMember subMemberStack[256];
4822    int subMemberStackPos = 0;
4823    uint64 bits = 0;
4824
4825    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4826    {
4827       // Don't recompute the instantiation...
4828       // Non Simple classes will have become constants by now
4829       if(inst.data)
4830          return;
4831
4832       if(_class.type == normalClass || _class.type == noHeadClass)
4833       {
4834          inst.data = (byte *)eInstance_New(_class);
4835          if(_class.type == normalClass)
4836             ((Instance)inst.data)._refCount++;
4837       }
4838       else
4839          inst.data = new0 byte[_class.structSize];
4840    }
4841
4842    if(inst.members)
4843    {
4844       for(members = inst.members->first; members; members = members.next)
4845       {
4846          switch(members.type)
4847          {
4848             case dataMembersInit:
4849             {
4850                if(members.dataMembers)
4851                {
4852                   MemberInit member;
4853                   for(member = members.dataMembers->first; member; member = member.next)
4854                   {
4855                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4856                      bool found = false;
4857
4858                      Property prop = null;
4859                      DataMember dataMember = null;
4860                      uint dataMemberOffset;
4861
4862                      if(!ident)
4863                      {
4864                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4865                         if(curMember)
4866                         {
4867                            if(curMember.isProperty)
4868                               prop = (Property)curMember;
4869                            else
4870                            {
4871                               dataMember = curMember;
4872
4873                               // CHANGED THIS HERE
4874                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4875
4876                               // 2013/17/29 -- It seems that this was missing here!
4877                               if(_class.type == normalClass)
4878                                  dataMemberOffset += _class.base.structSize;
4879                               // dataMemberOffset = dataMember.offset;
4880                            }
4881                            found = true;
4882                         }
4883                      }
4884                      else
4885                      {
4886                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4887                         if(prop)
4888                         {
4889                            found = true;
4890                            if(prop.memberAccess == publicAccess)
4891                            {
4892                               curMember = (DataMember)prop;
4893                               curClass = prop._class;
4894                            }
4895                         }
4896                         else
4897                         {
4898                            DataMember _subMemberStack[256];
4899                            int _subMemberStackPos = 0;
4900
4901                            // FILL MEMBER STACK
4902                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4903
4904                            if(dataMember)
4905                            {
4906                               found = true;
4907                               if(dataMember.memberAccess == publicAccess)
4908                               {
4909                                  curMember = dataMember;
4910                                  curClass = dataMember._class;
4911                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4912                                  subMemberStackPos = _subMemberStackPos;
4913                               }
4914                            }
4915                         }
4916                      }
4917
4918                      if(found && member.initializer && member.initializer.type == expInitializer)
4919                      {
4920                         Expression value = member.initializer.exp;
4921                         Type type = null;
4922                         bool deepMember = false;
4923                         if(prop)
4924                         {
4925                            type = prop.dataType;
4926                         }
4927                         else if(dataMember)
4928                         {
4929                            if(!dataMember.dataType)
4930                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4931
4932                            type = dataMember.dataType;
4933                         }
4934
4935                         if(ident && ident.next)
4936                         {
4937                            deepMember = true;
4938
4939                            // for(; ident && type; ident = ident.next)
4940                            for(ident = ident.next; ident && type; ident = ident.next)
4941                            {
4942                               if(type.kind == classType)
4943                               {
4944                                  prop = eClass_FindProperty(type._class.registered,
4945                                     ident.string, privateModule);
4946                                  if(prop)
4947                                     type = prop.dataType;
4948                                  else
4949                                  {
4950                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4951                                        ident.string, &dataMemberOffset, privateModule, null, null);
4952                                     if(dataMember)
4953                                        type = dataMember.dataType;
4954                                  }
4955                               }
4956                               else if(type.kind == structType || type.kind == unionType)
4957                               {
4958                                  Type memberType;
4959                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4960                                  {
4961                                     if(!strcmp(memberType.name, ident.string))
4962                                     {
4963                                        type = memberType;
4964                                        break;
4965                                     }
4966                                  }
4967                               }
4968                            }
4969                         }
4970                         if(value)
4971                         {
4972                            FreeType(value.destType);
4973                            value.destType = type;
4974                            if(type) type.refCount++;
4975                            ComputeExpression(value);
4976                         }
4977                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4978                         {
4979                            if(type.kind == classType)
4980                            {
4981                               Class _class = type._class.registered;
4982                               if(_class && (_class.type == bitClass || _class.type == unitClass || _class.type == enumClass))
4983                               {
4984                                  if(!_class.dataType)
4985                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4986                                  type = _class.dataType;
4987                               }
4988                            }
4989
4990                            if(dataMember)
4991                            {
4992                               void * ptr = inst.data + dataMemberOffset;
4993
4994                               if(value.type == constantExp)
4995                               {
4996                                  switch(type.kind)
4997                                  {
4998                                     case intType:
4999                                     {
5000                                        GetInt(value, (int*)ptr);
5001                                        break;
5002                                     }
5003                                     case int64Type:
5004                                     {
5005                                        GetInt64(value, (int64*)ptr);
5006                                        break;
5007                                     }
5008                                     case intPtrType:
5009                                     {
5010                                        GetIntPtr(value, (intptr*)ptr);
5011                                        break;
5012                                     }
5013                                     case intSizeType:
5014                                     {
5015                                        GetIntSize(value, (intsize*)ptr);
5016                                        break;
5017                                     }
5018                                     case floatType:
5019                                     {
5020                                        GetFloat(value, (float*)ptr);
5021                                        break;
5022                                     }
5023                                     case doubleType:
5024                                     {
5025                                        GetDouble(value, (double *)ptr);
5026                                        break;
5027                                     }
5028                                  }
5029                               }
5030                               else if(value.type == instanceExp)
5031                               {
5032                                  if(type.kind == classType)
5033                                  {
5034                                     Class _class = type._class.registered;
5035                                     if(_class.type == structClass)
5036                                     {
5037                                        ComputeTypeSize(type);
5038                                        if(value.instance.data)
5039                                           memcpy(ptr, value.instance.data, type.size);
5040                                     }
5041                                  }
5042                               }
5043                            }
5044                            else if(prop && prop.Set != (void *)(intptr)1)
5045                            {
5046                               if(value.type == instanceExp && value.instance.data)
5047                               {
5048                                  if(type.kind == classType)
5049                                  {
5050                                     Class _class = type._class.registered;
5051                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
5052                                     {
5053                                        void (*Set)(void *, void *) = (void *)prop.Set;
5054                                        Set(inst.data, value.instance.data);
5055                                        PopulateInstance(inst);
5056                                     }
5057                                  }
5058                               }
5059                               else if(value.type == constantExp)
5060                               {
5061                                  switch(type.kind)
5062                                  {
5063                                     case doubleType:
5064                                     {
5065                                        void (*Set)(void *, double) = (void *)prop.Set;
5066                                        Set(inst.data, strtod(value.constant, null) );
5067                                        break;
5068                                     }
5069                                     case floatType:
5070                                     {
5071                                        void (*Set)(void *, float) = (void *)prop.Set;
5072                                        Set(inst.data, (float)(strtod(value.constant, null)));
5073                                        break;
5074                                     }
5075                                     case intType:
5076                                     {
5077                                        void (*Set)(void *, int) = (void *)prop.Set;
5078                                        Set(inst.data, (int)strtol(value.constant, null, 0));
5079                                        break;
5080                                     }
5081                                     case int64Type:
5082                                     {
5083                                        void (*Set)(void *, int64) = (void *)prop.Set;
5084                                        Set(inst.data, _strtoi64(value.constant, null, 0));
5085                                        break;
5086                                     }
5087                                     case intPtrType:
5088                                     {
5089                                        void (*Set)(void *, intptr) = (void *)prop.Set;
5090                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
5091                                        break;
5092                                     }
5093                                     case intSizeType:
5094                                     {
5095                                        void (*Set)(void *, intsize) = (void *)prop.Set;
5096                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
5097                                        break;
5098                                     }
5099                                  }
5100                               }
5101                               else if(value.type == stringExp)
5102                               {
5103                                  char temp[1024];
5104                                  ReadString(temp, value.string);
5105                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
5106                               }
5107                            }
5108                         }
5109                         else if(!deepMember && type && _class.type == unitClass)
5110                         {
5111                            if(prop)
5112                            {
5113                               // Only support converting units to units for now...
5114                               if(value.type == constantExp)
5115                               {
5116                                  if(type.kind == classType)
5117                                  {
5118                                     Class _class = type._class.registered;
5119                                     if(_class.type == unitClass)
5120                                     {
5121                                        if(!_class.dataType)
5122                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5123                                        type = _class.dataType;
5124                                     }
5125                                  }
5126                                  // TODO: Assuming same base type for units...
5127                                  switch(type.kind)
5128                                  {
5129                                     case floatType:
5130                                     {
5131                                        float fValue;
5132                                        float (*Set)(float) = (void *)prop.Set;
5133                                        GetFloat(member.initializer.exp, &fValue);
5134                                        exp.constant = PrintFloat(Set(fValue));
5135                                        exp.type = constantExp;
5136                                        break;
5137                                     }
5138                                     case doubleType:
5139                                     {
5140                                        double dValue;
5141                                        double (*Set)(double) = (void *)prop.Set;
5142                                        GetDouble(member.initializer.exp, &dValue);
5143                                        exp.constant = PrintDouble(Set(dValue));
5144                                        exp.type = constantExp;
5145                                        break;
5146                                     }
5147                                  }
5148                               }
5149                            }
5150                         }
5151                         else if(!deepMember && type && _class.type == bitClass)
5152                         {
5153                            if(prop)
5154                            {
5155                               if(value.type == instanceExp && value.instance.data)
5156                               {
5157                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5158                                  bits = Set(value.instance.data);
5159                               }
5160                               else if(value.type == constantExp)
5161                               {
5162                               }
5163                            }
5164                            else if(dataMember)
5165                            {
5166                               BitMember bitMember = (BitMember) dataMember;
5167                               Type type;
5168                               uint64 part = 0;
5169                               bits = (bits & ~bitMember.mask);
5170                               if(!bitMember.dataType)
5171                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5172                               type = bitMember.dataType;
5173                               if(type.kind == classType && type._class && type._class.registered)
5174                               {
5175                                  if(!type._class.registered.dataType)
5176                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5177                                  type = type._class.registered.dataType;
5178                               }
5179                               switch(type.kind)
5180                               {
5181                                  case _BoolType:
5182                                  case charType:       { byte v; type.isSigned ? GetChar(value, (char *)&v) : GetUChar(value, &v); part = (uint64)v; break; }
5183                                  case shortType:      { uint16 v; type.isSigned ? GetShort(value, (short *)&v) : GetUShort(value, &v); part = (uint64)v; break; }
5184                                  case intType:
5185                                  case longType:       { uint v; type.isSigned ? GetInt(value, (int *)&v) : GetUInt(value, &v); part = (uint64)v; break; }
5186                                  case int64Type:      { uint64 v; type.isSigned ? GetInt64(value, (int64 *)&v) : GetUInt64(value, &v); part = (uint64)v; break; }
5187                                  case intPtrType:     { uintptr v; type.isSigned ? GetIntPtr(value, (intptr *)&v) : GetUIntPtr(value, &v); part = (uint64)v; break; }
5188                                  case intSizeType:    { uintsize v; type.isSigned ? GetIntSize(value, (intsize *)&v) : GetUIntSize(value, &v); part = (uint64)v; break; }
5189                               }
5190                               bits |= part << bitMember.pos;
5191                            }
5192                         }
5193                      }
5194                      else
5195                      {
5196                         if(_class && _class.type == unitClass)
5197                         {
5198                            ComputeExpression(member.initializer.exp);
5199                            exp.constant = member.initializer.exp.constant;
5200                            exp.type = constantExp;
5201
5202                            member.initializer.exp.constant = null;
5203                         }
5204                      }
5205                   }
5206                }
5207                break;
5208             }
5209          }
5210       }
5211    }
5212    if(_class && _class.type == bitClass)
5213    {
5214       exp.constant = PrintHexUInt(bits);
5215       exp.type = constantExp;
5216    }
5217    if(exp.type != instanceExp)
5218    {
5219       FreeInstance(inst);
5220    }
5221 }
5222
5223 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5224 {
5225    bool result = false;
5226    switch(kind)
5227    {
5228       case shortType:
5229          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5230             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5231          break;
5232       case intType:
5233       case longType:
5234          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5235             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5236          break;
5237       case int64Type:
5238          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5239             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5240             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5241          break;
5242       case floatType:
5243          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5244             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5245             result = GetOpFloat(op, &op.f);
5246          break;
5247       case doubleType:
5248          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5249             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5250             result = GetOpDouble(op, &op.d);
5251          break;
5252       case pointerType:
5253          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5254             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5255             result = GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5256          break;
5257       case enumType:
5258          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5259             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5260             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5261          break;
5262       case intPtrType:
5263          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5264             result = isSigned ? GetOpInt64 /*GetOpIntPtr*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntPtr*/(op, &op.ui64);
5265          break;
5266       case intSizeType:
5267          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5268             result = isSigned ? GetOpInt64 /*GetOpIntSize*/(op, &op.i64) : GetOpUInt64 /*GetOpUIntSize*/(op, &op.ui64);
5269          break;
5270    }
5271    return result;
5272 }
5273
5274 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5275 {
5276    if(exp.op.op == SIZEOF)
5277    {
5278       FreeExpContents(exp);
5279       exp.type = constantExp;
5280       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5281    }
5282    else
5283    {
5284       if(!exp.op.exp1)
5285       {
5286          switch(exp.op.op)
5287          {
5288             // unary arithmetic
5289             case '+':
5290             {
5291                // Provide default unary +
5292                Expression exp2 = exp.op.exp2;
5293                exp.op.exp2 = null;
5294                FreeExpContents(exp);
5295                FreeType(exp.expType);
5296                FreeType(exp.destType);
5297                *exp = *exp2;
5298                delete exp2;
5299                break;
5300             }
5301             case '-':
5302                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5303                break;
5304             // unary arithmetic increment and decrement
5305                   //OPERATOR_ALL(UNARY, ++, Inc)
5306                   //OPERATOR_ALL(UNARY, --, Dec)
5307             // unary bitwise
5308             case '~':
5309                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5310                break;
5311             // unary logical negation
5312             case '!':
5313                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5314                break;
5315          }
5316       }
5317       else
5318       {
5319          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5320          {
5321             if(Promote(op2, op1.kind, op1.type.isSigned))
5322                op2.kind = op1.kind, op2.ops = op1.ops;
5323             else if(Promote(op1, op2.kind, op2.type.isSigned))
5324                op1.kind = op2.kind, op1.ops = op2.ops;
5325          }
5326          switch(exp.op.op)
5327          {
5328             // binary arithmetic
5329             case '+':
5330                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5331                break;
5332             case '-':
5333                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5334                break;
5335             case '*':
5336                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5337                break;
5338             case '/':
5339                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5340                break;
5341             case '%':
5342                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5343                break;
5344             // binary arithmetic assignment
5345                   //OPERATOR_ALL(BINARY, =, Asign)
5346                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5347                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5348                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5349                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5350                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5351             // binary bitwise
5352             case '&':
5353                if(exp.op.exp2)
5354                {
5355                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5356                }
5357                break;
5358             case '|':
5359                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5360                break;
5361             case '^':
5362                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5363                break;
5364             case LEFT_OP:
5365                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5366                break;
5367             case RIGHT_OP:
5368                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5369                break;
5370             // binary bitwise assignment
5371                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5372                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5373                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5374                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5375                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5376             // binary logical equality
5377             case EQ_OP:
5378                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5379                break;
5380             case NE_OP:
5381                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5382                break;
5383             // binary logical
5384             case AND_OP:
5385                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5386                break;
5387             case OR_OP:
5388                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5389                break;
5390             // binary logical relational
5391             case '>':
5392                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5393                break;
5394             case '<':
5395                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5396                break;
5397             case GE_OP:
5398                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5399                break;
5400             case LE_OP:
5401                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5402                break;
5403          }
5404       }
5405    }
5406 }
5407
5408 void ComputeExpression(Expression exp)
5409 {
5410 #ifdef _DEBUG
5411    char expString[10240];
5412    expString[0] = '\0';
5413    PrintExpression(exp, expString);
5414 #endif
5415
5416    switch(exp.type)
5417    {
5418       case identifierExp:
5419       {
5420          Identifier id = exp.identifier;
5421          if(id && exp.isConstant && !inCompiler && !inPreCompiler && !inDebugger)
5422          {
5423             Class c = (exp.expType && exp.expType.kind == classType && exp.expType._class) ? exp.expType._class.registered : null;
5424             if(c && c.type == enumClass)
5425             {
5426                Class enumClass = eSystem_FindClass(privateModule, "enum");
5427                if(enumClass)
5428                {
5429                   NamedLink64 value;
5430                   EnumClassData e = ACCESS_CLASSDATA(c, enumClass);
5431                   for(value = e.values.first; value; value = value.next)
5432                   {
5433                      if(!strcmp(value.name, id.string))
5434                         break;
5435                   }
5436                   if(value)
5437                   {
5438                      const String dts = c.dataTypeString;
5439                      FreeExpContents(exp);
5440                      exp.type = constantExp;
5441                      exp.constant = (dts && (!strcmp(dts, "int") || !strcmp(dts, "int64") || !strcmp(dts, "short") || !strcmp(dts, "char"))) ? PrintInt64(value.data) : PrintUInt64(value.data);
5442                   }
5443                }
5444             }
5445          }
5446          break;
5447       }
5448       case instanceExp:
5449       {
5450          ComputeInstantiation(exp);
5451          break;
5452       }
5453       /*
5454       case constantExp:
5455          break;
5456       */
5457       case opExp:
5458       {
5459          Expression exp1, exp2 = null;
5460          Operand op1 { };
5461          Operand op2 { };
5462
5463          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5464          if(exp.op.exp2)
5465          {
5466             Expression e = exp.op.exp2;
5467
5468             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5469             {
5470                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5471                {
5472                   if(e.type == extensionCompoundExp)
5473                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5474                   else
5475                      e = e.list->last;
5476                }
5477             }
5478             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5479             {
5480                if(e.type == stringExp && e.string)
5481                {
5482                   char * string = e.string;
5483                   int len = strlen(string);
5484                   char * tmp = new char[len-2+1];
5485                   len = UnescapeString(tmp, string + 1, len - 2);
5486                   delete tmp;
5487                   FreeExpContents(exp);
5488                   exp.type = constantExp;
5489                   exp.constant = PrintUInt(len + 1);
5490                }
5491                else
5492                {
5493                   Type type = e.expType;
5494                   type.refCount++;
5495                   FreeExpContents(exp);
5496                   exp.type = constantExp;
5497                   exp.constant = PrintUInt(ComputeTypeSize(type));
5498                   FreeType(type);
5499                }
5500                break;
5501             }
5502             else
5503                ComputeExpression(exp.op.exp2);
5504          }
5505          if(exp.op.exp1)
5506          {
5507             ComputeExpression(exp.op.exp1);
5508             exp1 = exp.op.exp1;
5509             exp2 = exp.op.exp2;
5510             op1 = GetOperand(exp1);
5511             if(op1.type) op1.type.refCount++;
5512             if(exp2)
5513             {
5514                op2 = GetOperand(exp2);
5515                if(op2.type) op2.type.refCount++;
5516             }
5517          }
5518          else
5519          {
5520             exp1 = exp.op.exp2;
5521             op1 = GetOperand(exp1);
5522             if(op1.type) op1.type.refCount++;
5523          }
5524
5525          CallOperator(exp, exp1, exp2, op1, op2);
5526          /*
5527          switch(exp.op.op)
5528          {
5529             // Unary operators
5530             case '&':
5531                // Also binary
5532                if(exp.op.exp1 && exp.op.exp2)
5533                {
5534                   // Binary And
5535                   if(op1.ops.BitAnd)
5536                   {
5537                      FreeExpContents(exp);
5538                      op1.ops.BitAnd(exp, op1, op2);
5539                   }
5540                }
5541                break;
5542             case '*':
5543                if(exp.op.exp1)
5544                {
5545                   if(op1.ops.Mul)
5546                   {
5547                      FreeExpContents(exp);
5548                      op1.ops.Mul(exp, op1, op2);
5549                   }
5550                }
5551                break;
5552             case '+':
5553                if(exp.op.exp1)
5554                {
5555                   if(op1.ops.Add)
5556                   {
5557                      FreeExpContents(exp);
5558                      op1.ops.Add(exp, op1, op2);
5559                   }
5560                }
5561                else
5562                {
5563                   // Provide default unary +
5564                   Expression exp2 = exp.op.exp2;
5565                   exp.op.exp2 = null;
5566                   FreeExpContents(exp);
5567                   FreeType(exp.expType);
5568                   FreeType(exp.destType);
5569
5570                   *exp = *exp2;
5571                   delete exp2;
5572                }
5573                break;
5574             case '-':
5575                if(exp.op.exp1)
5576                {
5577                   if(op1.ops.Sub)
5578                   {
5579                      FreeExpContents(exp);
5580                      op1.ops.Sub(exp, op1, op2);
5581                   }
5582                }
5583                else
5584                {
5585                   if(op1.ops.Neg)
5586                   {
5587                      FreeExpContents(exp);
5588                      op1.ops.Neg(exp, op1);
5589                   }
5590                }
5591                break;
5592             case '~':
5593                if(op1.ops.BitNot)
5594                {
5595                   FreeExpContents(exp);
5596                   op1.ops.BitNot(exp, op1);
5597                }
5598                break;
5599             case '!':
5600                if(op1.ops.Not)
5601                {
5602                   FreeExpContents(exp);
5603                   op1.ops.Not(exp, op1);
5604                }
5605                break;
5606             // Binary only operators
5607             case '/':
5608                if(op1.ops.Div)
5609                {
5610                   FreeExpContents(exp);
5611                   op1.ops.Div(exp, op1, op2);
5612                }
5613                break;
5614             case '%':
5615                if(op1.ops.Mod)
5616                {
5617                   FreeExpContents(exp);
5618                   op1.ops.Mod(exp, op1, op2);
5619                }
5620                break;
5621             case LEFT_OP:
5622                break;
5623             case RIGHT_OP:
5624                break;
5625             case '<':
5626                if(exp.op.exp1)
5627                {
5628                   if(op1.ops.Sma)
5629                   {
5630                      FreeExpContents(exp);
5631                      op1.ops.Sma(exp, op1, op2);
5632                   }
5633                }
5634                break;
5635             case '>':
5636                if(exp.op.exp1)
5637                {
5638                   if(op1.ops.Grt)
5639                   {
5640                      FreeExpContents(exp);
5641                      op1.ops.Grt(exp, op1, op2);
5642                   }
5643                }
5644                break;
5645             case LE_OP:
5646                if(exp.op.exp1)
5647                {
5648                   if(op1.ops.SmaEqu)
5649                   {
5650                      FreeExpContents(exp);
5651                      op1.ops.SmaEqu(exp, op1, op2);
5652                   }
5653                }
5654                break;
5655             case GE_OP:
5656                if(exp.op.exp1)
5657                {
5658                   if(op1.ops.GrtEqu)
5659                   {
5660                      FreeExpContents(exp);
5661                      op1.ops.GrtEqu(exp, op1, op2);
5662                   }
5663                }
5664                break;
5665             case EQ_OP:
5666                if(exp.op.exp1)
5667                {
5668                   if(op1.ops.Equ)
5669                   {
5670                      FreeExpContents(exp);
5671                      op1.ops.Equ(exp, op1, op2);
5672                   }
5673                }
5674                break;
5675             case NE_OP:
5676                if(exp.op.exp1)
5677                {
5678                   if(op1.ops.Nqu)
5679                   {
5680                      FreeExpContents(exp);
5681                      op1.ops.Nqu(exp, op1, op2);
5682                   }
5683                }
5684                break;
5685             case '|':
5686                if(op1.ops.BitOr)
5687                {
5688                   FreeExpContents(exp);
5689                   op1.ops.BitOr(exp, op1, op2);
5690                }
5691                break;
5692             case '^':
5693                if(op1.ops.BitXor)
5694                {
5695                   FreeExpContents(exp);
5696                   op1.ops.BitXor(exp, op1, op2);
5697                }
5698                break;
5699             case AND_OP:
5700                break;
5701             case OR_OP:
5702                break;
5703             case SIZEOF:
5704                FreeExpContents(exp);
5705                exp.type = constantExp;
5706                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5707                break;
5708          }
5709          */
5710          if(op1.type) FreeType(op1.type);
5711          if(op2.type) FreeType(op2.type);
5712          break;
5713       }
5714       case bracketsExp:
5715       case extensionExpressionExp:
5716       {
5717          Expression e, n;
5718          for(e = exp.list->first; e; e = n)
5719          {
5720             n = e.next;
5721             if(!n)
5722             {
5723                OldList * list = exp.list;
5724                Expression prev = exp.prev;
5725                Expression next = exp.next;
5726                ComputeExpression(e);
5727                //FreeExpContents(exp);
5728                FreeType(exp.expType);
5729                FreeType(exp.destType);
5730                *exp = *e;
5731                exp.prev = prev;
5732                exp.next = next;
5733                delete e;
5734                delete list;
5735             }
5736             else
5737             {
5738                FreeExpression(e);
5739             }
5740          }
5741          break;
5742       }
5743       /*
5744
5745       case ExpIndex:
5746       {
5747          Expression e;
5748          exp.isConstant = true;
5749
5750          ComputeExpression(exp.index.exp);
5751          if(!exp.index.exp.isConstant)
5752             exp.isConstant = false;
5753
5754          for(e = exp.index.index->first; e; e = e.next)
5755          {
5756             ComputeExpression(e);
5757             if(!e.next)
5758             {
5759                // Check if this type is int
5760             }
5761             if(!e.isConstant)
5762                exp.isConstant = false;
5763          }
5764          exp.expType = Dereference(exp.index.exp.expType);
5765          break;
5766       }
5767       */
5768       case memberExp:
5769       {
5770          Expression memberExp = exp.member.exp;
5771          Identifier memberID = exp.member.member;
5772
5773          Type type;
5774          ComputeExpression(exp.member.exp);
5775          type = exp.member.exp.expType;
5776          if(type)
5777          {
5778             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);
5779             Property prop = null;
5780             DataMember member = null;
5781             Class convertTo = null;
5782             if(type.kind == subClassType && exp.member.exp.type == classExp)
5783                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5784
5785             if(!_class)
5786             {
5787                char string[256];
5788                Symbol classSym;
5789                string[0] = '\0';
5790                PrintTypeNoConst(type, string, false, true);
5791                classSym = FindClass(string);
5792                _class = classSym ? classSym.registered : null;
5793             }
5794
5795             if(exp.member.member)
5796             {
5797                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5798                if(!prop)
5799                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5800             }
5801             if(!prop && !member && _class && exp.member.member)
5802             {
5803                Symbol classSym = FindClass(exp.member.member.string);
5804                convertTo = _class;
5805                _class = classSym ? classSym.registered : null;
5806                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5807             }
5808
5809             if(prop)
5810             {
5811                if(prop.compiled)
5812                {
5813                   Type type = prop.dataType;
5814                   // TODO: Assuming same base type for units...
5815                   if(_class.type == unitClass)
5816                   {
5817                      if(type.kind == classType)
5818                      {
5819                         Class _class = type._class.registered;
5820                         if(_class.type == unitClass)
5821                         {
5822                            if(!_class.dataType)
5823                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5824                            type = _class.dataType;
5825                         }
5826                      }
5827                      switch(type.kind)
5828                      {
5829                         case floatType:
5830                         {
5831                            float value;
5832                            float (*Get)(float) = (void *)prop.Get;
5833                            GetFloat(exp.member.exp, &value);
5834                            exp.constant = PrintFloat(Get ? Get(value) : value);
5835                            exp.type = constantExp;
5836                            break;
5837                         }
5838                         case doubleType:
5839                         {
5840                            double value;
5841                            double (*Get)(double);
5842                            GetDouble(exp.member.exp, &value);
5843
5844                            if(convertTo)
5845                               Get = (void *)prop.Set;
5846                            else
5847                               Get = (void *)prop.Get;
5848                            exp.constant = PrintDouble(Get ? Get(value) : value);
5849                            exp.type = constantExp;
5850                            break;
5851                         }
5852                      }
5853                   }
5854                   else
5855                   {
5856                      if(convertTo)
5857                      {
5858                         Expression value = exp.member.exp;
5859                         Type type;
5860                         if(!prop.dataType)
5861                            ProcessPropertyType(prop);
5862
5863                         type = prop.dataType;
5864                         if(!type)
5865                         {
5866                             // printf("Investigate this\n");
5867                         }
5868                         else if(_class.type == structClass)
5869                         {
5870                            switch(type.kind)
5871                            {
5872                               case classType:
5873                               {
5874                                  Class propertyClass = type._class.registered;
5875                                  if(propertyClass.type == structClass && value.type == instanceExp)
5876                                  {
5877                                     void (*Set)(void *, void *) = (void *)prop.Set;
5878                                     exp.instance = Instantiation { };
5879                                     exp.instance.data = new0 byte[_class.structSize];
5880                                     exp.instance._class = MkSpecifierName(_class.fullName);
5881                                     exp.instance.loc = exp.loc;
5882                                     exp.type = instanceExp;
5883                                     Set(exp.instance.data, value.instance.data);
5884                                     PopulateInstance(exp.instance);
5885                                  }
5886                                  break;
5887                               }
5888                               case intType:
5889                               {
5890                                  int intValue;
5891                                  void (*Set)(void *, int) = (void *)prop.Set;
5892
5893                                  exp.instance = Instantiation { };
5894                                  exp.instance.data = new0 byte[_class.structSize];
5895                                  exp.instance._class = MkSpecifierName(_class.fullName);
5896                                  exp.instance.loc = exp.loc;
5897                                  exp.type = instanceExp;
5898
5899                                  GetInt(value, &intValue);
5900
5901                                  Set(exp.instance.data, intValue);
5902                                  PopulateInstance(exp.instance);
5903                                  break;
5904                               }
5905                               case int64Type:
5906                               {
5907                                  int64 intValue;
5908                                  void (*Set)(void *, int64) = (void *)prop.Set;
5909
5910                                  exp.instance = Instantiation { };
5911                                  exp.instance.data = new0 byte[_class.structSize];
5912                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5913                                  exp.instance.loc = exp.loc;
5914                                  exp.type = instanceExp;
5915
5916                                  GetInt64(value, &intValue);
5917
5918                                  Set(exp.instance.data, intValue);
5919                                  PopulateInstance(exp.instance);
5920                                  break;
5921                               }
5922                               case intPtrType:
5923                               {
5924                                  // TOFIX:
5925                                  intptr intValue;
5926                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5927
5928                                  exp.instance = Instantiation { };
5929                                  exp.instance.data = new0 byte[_class.structSize];
5930                                  exp.instance._class = MkSpecifierName(_class.fullName);
5931                                  exp.instance.loc = exp.loc;
5932                                  exp.type = instanceExp;
5933
5934                                  GetIntPtr(value, &intValue);
5935
5936                                  Set(exp.instance.data, intValue);
5937                                  PopulateInstance(exp.instance);
5938                                  break;
5939                               }
5940                               case intSizeType:
5941                               {
5942                                  // TOFIX:
5943                                  intsize intValue;
5944                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5945
5946                                  exp.instance = Instantiation { };
5947                                  exp.instance.data = new0 byte[_class.structSize];
5948                                  exp.instance._class = MkSpecifierName(_class.fullName);
5949                                  exp.instance.loc = exp.loc;
5950                                  exp.type = instanceExp;
5951
5952                                  GetIntSize(value, &intValue);
5953
5954                                  Set(exp.instance.data, intValue);
5955                                  PopulateInstance(exp.instance);
5956                                  break;
5957                               }
5958                               case floatType:
5959                               {
5960                                  float floatValue;
5961                                  void (*Set)(void *, float) = (void *)prop.Set;
5962
5963                                  exp.instance = Instantiation { };
5964                                  exp.instance.data = new0 byte[_class.structSize];
5965                                  exp.instance._class = MkSpecifierName(_class.fullName);
5966                                  exp.instance.loc = exp.loc;
5967                                  exp.type = instanceExp;
5968
5969                                  GetFloat(value, &floatValue);
5970
5971                                  Set(exp.instance.data, floatValue);
5972                                  PopulateInstance(exp.instance);
5973                                  break;
5974                               }
5975                               case doubleType:
5976                               {
5977                                  double doubleValue;
5978                                  void (*Set)(void *, double) = (void *)prop.Set;
5979
5980                                  exp.instance = Instantiation { };
5981                                  exp.instance.data = new0 byte[_class.structSize];
5982                                  exp.instance._class = MkSpecifierName(_class.fullName);
5983                                  exp.instance.loc = exp.loc;
5984                                  exp.type = instanceExp;
5985
5986                                  GetDouble(value, &doubleValue);
5987
5988                                  Set(exp.instance.data, doubleValue);
5989                                  PopulateInstance(exp.instance);
5990                                  break;
5991                               }
5992                            }
5993                         }
5994                         else if(_class.type == bitClass)
5995                         {
5996                            switch(type.kind)
5997                            {
5998                               case classType:
5999                               {
6000                                  Class propertyClass = type._class.registered;
6001                                  if(propertyClass.type == structClass && value.instance.data)
6002                                  {
6003                                     unsigned int (*Set)(void *) = (void *)prop.Set;
6004                                     unsigned int bits = Set(value.instance.data);
6005                                     exp.constant = PrintHexUInt(bits);
6006                                     exp.type = constantExp;
6007                                     break;
6008                                  }
6009                                  else if(_class.type == bitClass)
6010                                  {
6011                                     unsigned int value;
6012                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
6013                                     unsigned int bits;
6014
6015                                     GetUInt(exp.member.exp, &value);
6016                                     bits = Set(value);
6017                                     exp.constant = PrintHexUInt(bits);
6018                                     exp.type = constantExp;
6019                                  }
6020                               }
6021                            }
6022                         }
6023                      }
6024                      else
6025                      {
6026                         if(_class.type == bitClass)
6027                         {
6028                            unsigned int value;
6029                            GetUInt(exp.member.exp, &value);
6030
6031                            switch(type.kind)
6032                            {
6033                               case classType:
6034                               {
6035                                  Class _class = type._class.registered;
6036                                  if(_class.type == structClass)
6037                                  {
6038                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
6039
6040                                     exp.instance = Instantiation { };
6041                                     exp.instance.data = new0 byte[_class.structSize];
6042                                     exp.instance._class = MkSpecifierName(_class.fullName);
6043                                     exp.instance.loc = exp.loc;
6044                                     //exp.instance.fullSet = true;
6045                                     exp.type = instanceExp;
6046                                     Get(value, exp.instance.data);
6047                                     PopulateInstance(exp.instance);
6048                                  }
6049                                  else if(_class.type == bitClass)
6050                                  {
6051                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
6052                                     uint64 bits = Get(value);
6053                                     exp.constant = PrintHexUInt64(bits);
6054                                     exp.type = constantExp;
6055                                  }
6056                                  break;
6057                               }
6058                            }
6059                         }
6060                         else if(_class.type == structClass)
6061                         {
6062                            byte * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
6063                            switch(type.kind)
6064                            {
6065                               case classType:
6066                               {
6067                                  Class _class = type._class.registered;
6068                                  if(_class.type == structClass && value)
6069                                  {
6070                                     void (*Get)(void *, void *) = (void *)prop.Get;
6071
6072                                     exp.instance = Instantiation { };
6073                                     exp.instance.data = new0 byte[_class.structSize];
6074                                     exp.instance._class = MkSpecifierName(_class.fullName);
6075                                     exp.instance.loc = exp.loc;
6076                                     //exp.instance.fullSet = true;
6077                                     exp.type = instanceExp;
6078                                     Get(value, exp.instance.data);
6079                                     PopulateInstance(exp.instance);
6080                                  }
6081                                  break;
6082                               }
6083                            }
6084                         }
6085                         /*else
6086                         {
6087                            char * value = exp.member.exp.instance.data;
6088                            switch(type.kind)
6089                            {
6090                               case classType:
6091                               {
6092                                  Class _class = type._class.registered;
6093                                  if(_class.type == normalClass)
6094                                  {
6095                                     void *(*Get)(void *) = (void *)prop.Get;
6096
6097                                     exp.instance = Instantiation { };
6098                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
6099                                     exp.type = instanceExp;
6100                                     exp.instance.data = Get(value, exp.instance.data);
6101                                  }
6102                                  break;
6103                               }
6104                            }
6105                         }
6106                         */
6107                      }
6108                   }
6109                }
6110                else
6111                {
6112                   exp.isConstant = false;
6113                }
6114             }
6115             else if(member)
6116             {
6117             }
6118          }
6119
6120          if(exp.type != ExpressionType::memberExp)
6121          {
6122             FreeExpression(memberExp);
6123             FreeIdentifier(memberID);
6124          }
6125          break;
6126       }
6127       case typeSizeExp:
6128       {
6129          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
6130          FreeExpContents(exp);
6131          exp.constant = PrintUInt(ComputeTypeSize(type));
6132          exp.type = constantExp;
6133          FreeType(type);
6134          break;
6135       }
6136       case classSizeExp:
6137       {
6138          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6139          if(classSym && classSym.registered)
6140          {
6141             if(classSym.registered.fixed)
6142             {
6143                FreeSpecifier(exp._class);
6144                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6145                exp.type = constantExp;
6146             }
6147             else
6148             {
6149                char className[1024];
6150                strcpy(className, "__ecereClass_");
6151                FullClassNameCat(className, classSym.string, true);
6152
6153                DeclareClass(curExternal, classSym, className);
6154
6155                FreeExpContents(exp);
6156                exp.type = pointerExp;
6157                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6158                exp.member.member = MkIdentifier("structSize");
6159             }
6160          }
6161          break;
6162       }
6163       case castExp:
6164       //case constantExp:
6165       {
6166          Type type;
6167          Expression e = exp;
6168          if(exp.type == castExp)
6169          {
6170             if(exp.cast.exp)
6171                ComputeExpression(exp.cast.exp);
6172             e = exp.cast.exp;
6173          }
6174          if(e && exp.expType)
6175          {
6176             /*if(exp.destType)
6177                type = exp.destType;
6178             else*/
6179                type = exp.expType;
6180             if(type.kind == classType)
6181             {
6182                Class _class = type._class.registered;
6183                if(_class && (_class.type == unitClass || _class.type == bitClass))
6184                {
6185                   if(!_class.dataType)
6186                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6187                   type = _class.dataType;
6188                }
6189             }
6190
6191             switch(type.kind)
6192             {
6193                case _BoolType:
6194                case charType:
6195                   if(type.isSigned)
6196                   {
6197                      char value = 0;
6198                      if(GetChar(e, &value))
6199                      {
6200                         FreeExpContents(exp);
6201                         exp.constant = PrintChar(value);
6202                         exp.type = constantExp;
6203                      }
6204                   }
6205                   else
6206                   {
6207                      unsigned char value = 0;
6208                      if(GetUChar(e, &value))
6209                      {
6210                         FreeExpContents(exp);
6211                         exp.constant = PrintUChar(value);
6212                         exp.type = constantExp;
6213                      }
6214                   }
6215                   break;
6216                case shortType:
6217                   if(type.isSigned)
6218                   {
6219                      short value = 0;
6220                      if(GetShort(e, &value))
6221                      {
6222                         FreeExpContents(exp);
6223                         exp.constant = PrintShort(value);
6224                         exp.type = constantExp;
6225                      }
6226                   }
6227                   else
6228                   {
6229                      unsigned short value = 0;
6230                      if(GetUShort(e, &value))
6231                      {
6232                         FreeExpContents(exp);
6233                         exp.constant = PrintUShort(value);
6234                         exp.type = constantExp;
6235                      }
6236                   }
6237                   break;
6238                case intType:
6239                   if(type.isSigned)
6240                   {
6241                      int value = 0;
6242                      if(GetInt(e, &value))
6243                      {
6244                         FreeExpContents(exp);
6245                         exp.constant = PrintInt(value);
6246                         exp.type = constantExp;
6247                      }
6248                   }
6249                   else
6250                   {
6251                      unsigned int value = 0;
6252                      if(GetUInt(e, &value))
6253                      {
6254                         FreeExpContents(exp);
6255                         exp.constant = PrintUInt(value);
6256                         exp.type = constantExp;
6257                      }
6258                   }
6259                   break;
6260                case int64Type:
6261                   if(type.isSigned)
6262                   {
6263                      int64 value = 0;
6264                      if(GetInt64(e, &value))
6265                      {
6266                         FreeExpContents(exp);
6267                         exp.constant = PrintInt64(value);
6268                         exp.type = constantExp;
6269                      }
6270                   }
6271                   else
6272                   {
6273                      uint64 value = 0;
6274                      if(GetUInt64(e, &value))
6275                      {
6276                         FreeExpContents(exp);
6277                         exp.constant = PrintUInt64(value);
6278                         exp.type = constantExp;
6279                      }
6280                   }
6281                   break;
6282                case intPtrType:
6283                   if(type.isSigned)
6284                   {
6285                      intptr value = 0;
6286                      if(GetIntPtr(e, &value))
6287                      {
6288                         FreeExpContents(exp);
6289                         exp.constant = PrintInt64((int64)value);
6290                         exp.type = constantExp;
6291                      }
6292                   }
6293                   else
6294                   {
6295                      uintptr value = 0;
6296                      if(GetUIntPtr(e, &value))
6297                      {
6298                         FreeExpContents(exp);
6299                         exp.constant = PrintUInt64((uint64)value);
6300                         exp.type = constantExp;
6301                      }
6302                   }
6303                   break;
6304                case intSizeType:
6305                   if(type.isSigned)
6306                   {
6307                      intsize value = 0;
6308                      if(GetIntSize(e, &value))
6309                      {
6310                         FreeExpContents(exp);
6311                         exp.constant = PrintInt64((int64)value);
6312                         exp.type = constantExp;
6313                      }
6314                   }
6315                   else
6316                   {
6317                      uintsize value = 0;
6318                      if(GetUIntSize(e, &value))
6319                      {
6320                         FreeExpContents(exp);
6321                         exp.constant = PrintUInt64((uint64)value);
6322                         exp.type = constantExp;
6323                      }
6324                   }
6325                   break;
6326                case floatType:
6327                {
6328                   float value = 0;
6329                   if(GetFloat(e, &value))
6330                   {
6331                      FreeExpContents(exp);
6332                      exp.constant = PrintFloat(value);
6333                      exp.type = constantExp;
6334                   }
6335                   break;
6336                }
6337                case doubleType:
6338                {
6339                   double value = 0;
6340                   if(GetDouble(e, &value))
6341                   {
6342                      FreeExpContents(exp);
6343                      exp.constant = PrintDouble(value);
6344                      exp.type = constantExp;
6345                   }
6346                   break;
6347                }
6348             }
6349          }
6350          break;
6351       }
6352       case conditionExp:
6353       {
6354          Operand op1 { };
6355          Operand op2 { };
6356          Operand op3 { };
6357
6358          if(exp.cond.exp)
6359             // Caring only about last expression for now...
6360             ComputeExpression(exp.cond.exp->last);
6361          if(exp.cond.elseExp)
6362             ComputeExpression(exp.cond.elseExp);
6363          if(exp.cond.cond)
6364             ComputeExpression(exp.cond.cond);
6365
6366          op1 = GetOperand(exp.cond.cond);
6367          if(op1.type) op1.type.refCount++;
6368          op2 = GetOperand(exp.cond.exp->last);
6369          if(op2.type) op2.type.refCount++;
6370          op3 = GetOperand(exp.cond.elseExp);
6371          if(op3.type) op3.type.refCount++;
6372
6373          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6374          if(op1.type) FreeType(op1.type);
6375          if(op2.type) FreeType(op2.type);
6376          if(op3.type) FreeType(op3.type);
6377          break;
6378       }
6379    }
6380 }
6381
6382 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla, bool warnConst)
6383 {
6384    bool result = true;
6385    if(destType)
6386    {
6387       OldList converts { };
6388       Conversion convert;
6389
6390       if(destType.kind == voidType)
6391          return false;
6392
6393       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla, warnConst))
6394          result = false;
6395       if(converts.count)
6396       {
6397          // for(convert = converts.last; convert; convert = convert.prev)
6398          for(convert = converts.first; convert; convert = convert.next)
6399          {
6400             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6401             if(!empty)
6402             {
6403                Expression newExp { };
6404                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6405
6406                // TODO: Check this...
6407                *newExp = *exp;
6408                newExp.prev = null;
6409                newExp.next = null;
6410                newExp.destType = null;
6411
6412                if(convert.isGet)
6413                {
6414                   // [exp].ColorRGB
6415                   exp.type = memberExp;
6416                   exp.addedThis = true;
6417                   exp.member.exp = newExp;
6418                   FreeType(exp.member.exp.expType);
6419
6420                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6421                   exp.member.exp.expType.classObjectType = objectType;
6422                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6423                   exp.member.memberType = propertyMember;
6424                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6425                   // TESTING THIS... for (int)degrees
6426                   exp.needCast = true;
6427                   if(exp.expType) exp.expType.refCount++;
6428                   ApplyAnyObjectLogic(exp.member.exp);
6429                }
6430                else
6431                {
6432
6433                   /*if(exp.isConstant)
6434                   {
6435                      // Color { ColorRGB = [exp] };
6436                      exp.type = instanceExp;
6437                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6438                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6439                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6440                   }
6441                   else*/
6442                   {
6443                      // If not constant, don't turn it yet into an instantiation
6444                      // (Go through the deep members system first)
6445                      exp.type = memberExp;
6446                      exp.addedThis = true;
6447                      exp.member.exp = newExp;
6448
6449                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6450                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6451                         newExp.expType._class.registered.type == noHeadClass)
6452                      {
6453                         newExp.byReference = true;
6454                      }
6455
6456                      FreeType(exp.member.exp.expType);
6457                      /*exp.member.exp.expType = convert.convert.dataType;
6458                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6459                      exp.member.exp.expType = null;
6460                      if(convert.convert.dataType)
6461                      {
6462                         exp.member.exp.expType = { };
6463                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6464                         exp.member.exp.expType.refCount = 1;
6465                         exp.member.exp.expType.classObjectType = objectType;
6466                         ApplyAnyObjectLogic(exp.member.exp);
6467                      }
6468
6469                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6470                      exp.member.memberType = reverseConversionMember;
6471                      exp.expType = convert.resultType ? convert.resultType :
6472                         MkClassType(convert.convert._class.fullName);
6473                      exp.needCast = true;
6474                      if(convert.resultType) convert.resultType.refCount++;
6475                   }
6476                }
6477             }
6478             else
6479             {
6480                FreeType(exp.expType);
6481                if(convert.isGet)
6482                {
6483                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6484                   if(exp.destType.casted)
6485                      exp.needCast = true;
6486                   if(exp.expType) exp.expType.refCount++;
6487                }
6488                else
6489                {
6490                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6491                   if(exp.destType.casted)
6492                      exp.needCast = true;
6493                   if(convert.resultType)
6494                      convert.resultType.refCount++;
6495                }
6496             }
6497          }
6498          if(exp.isConstant && inCompiler)
6499             ComputeExpression(exp);
6500
6501          converts.Free(FreeConvert);
6502       }
6503
6504       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6505       {
6506          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false, warnConst);
6507       }
6508       if(!result && exp.expType && exp.destType)
6509       {
6510          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6511              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6512             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6513             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6514             result = true;
6515       }
6516    }
6517    // if(result) CheckTemplateTypes(exp);
6518    return result;
6519 }
6520
6521 void CheckTemplateTypes(Expression exp)
6522 {
6523    /*
6524    bool et = exp.expType ? exp.expType.passAsTemplate : false;
6525    bool dt = exp.destType ? exp.destType.passAsTemplate : false;
6526    */
6527    Expression nbExp = GetNonBracketsExp(exp);
6528    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate &&
6529       (nbExp == exp || nbExp.type != castExp))
6530    {
6531       Expression newExp { };
6532       Context context;
6533       TypeKind kind = exp.expType.kind;
6534       *newExp = *exp;
6535       if(exp.destType) exp.destType.refCount++;
6536       if(exp.expType)  exp.expType.refCount++;
6537       newExp.prev = null;
6538       newExp.next = null;
6539
6540       if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered)
6541       {
6542          Class c = exp.expType._class.registered;
6543          if(c.type == bitClass || c.type == enumClass || c.type == unitClass)
6544          {
6545             if(!c.dataType)
6546                c.dataType = ProcessTypeString(c.dataTypeString, false);
6547             kind = c.dataType.kind;
6548          }
6549       }
6550
6551       switch(kind)
6552       {
6553          case doubleType:
6554             if(exp.destType.classObjectType)
6555             {
6556                // We need to pass the address, just pass it along (Undo what was done above)
6557                if(exp.destType) exp.destType.refCount--;
6558                if(exp.expType)  exp.expType.refCount--;
6559                delete newExp;
6560             }
6561             else
6562             {
6563                // If we're looking for value:
6564                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6565                OldList * specs;
6566                OldList * unionDefs = MkList();
6567                OldList * statements = MkList();
6568                context = PushContext();
6569                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6570                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6571                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6572                exp.type = extensionCompoundExp;
6573                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6574                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6575                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6576                exp.compound.compound.context = context;
6577                PopContext(context);
6578             }
6579             break;
6580          default:
6581             exp.type = castExp;
6582             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6583             if((exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass) || exp.expType.isPointerType)
6584                exp.cast.exp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), MkExpBrackets(MkListOne(newExp)));
6585             else
6586                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6587             exp.needCast = true;
6588             break;
6589       }
6590    }
6591    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6592    {
6593       Expression newExp { };
6594       Context context;
6595       TypeKind kind = exp.expType.kind;
6596       *newExp = *exp;
6597       if(exp.destType) exp.destType.refCount++;
6598       if(exp.expType)  exp.expType.refCount++;
6599       newExp.prev = null;
6600       newExp.next = null;
6601
6602       if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered)
6603       {
6604          Class c = exp.expType._class.registered;
6605          if(c.type == bitClass || c.type == enumClass || c.type == unitClass)
6606          {
6607             if(!c.dataType)
6608                c.dataType = ProcessTypeString(c.dataTypeString, false);
6609             kind = c.dataType.kind;
6610          }
6611       }
6612
6613       switch(kind)
6614       {
6615          case doubleType:
6616             if(exp.destType.classObjectType)
6617             {
6618                // We need to pass the address, just pass it along (Undo what was done above)
6619                if(exp.destType) exp.destType.refCount--;
6620                if(exp.expType)  exp.expType.refCount--;
6621                delete newExp;
6622             }
6623             else
6624             {
6625                // If we're looking for value:
6626                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6627                OldList * specs;
6628                OldList * unionDefs = MkList();
6629                OldList * statements = MkList();
6630                context = PushContext();
6631                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6632                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6633                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6634                exp.type = extensionCompoundExp;
6635                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6636                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6637                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6638                exp.compound.compound.context = context;
6639                PopContext(context);
6640             }
6641             break;
6642          case classType:
6643          {
6644             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6645             {
6646                exp.type = bracketsExp;
6647
6648                newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6649                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6650                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6651                ProcessExpressionType(exp.list->first);
6652                break;
6653             }
6654             else
6655             {
6656                exp.type = bracketsExp;
6657                if(exp.expType.isPointerType)
6658                {
6659                   exp.needTemplateCast = 2;
6660                   newExp.needCast = true;
6661                   newExp.needTemplateCast = 2;
6662                   newExp = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), newExp);
6663                }
6664
6665                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6666                exp.needTemplateCast = 2;
6667                newExp.needCast = true;
6668                newExp.needTemplateCast = 2;
6669                ProcessExpressionType(exp.list->first);
6670                break;
6671             }
6672          }
6673          default:
6674          {
6675             if(exp.expType.kind == templateType)
6676             {
6677                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6678                if(type)
6679                {
6680                   FreeType(exp.destType);
6681                   FreeType(exp.expType);
6682                   delete newExp;
6683                   break;
6684                }
6685             }
6686             /*if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6687             {
6688                // When was this required?    Removed to address using templated values to pass to printf()
6689                exp.type = opExp;
6690                exp.op.op = '*';
6691                exp.op.exp1 = null;
6692                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6693                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6694             }
6695             else*/
6696             {
6697                char typeString[1024];
6698                Declarator decl;
6699                OldList * specs = MkList();
6700                typeString[0] = '\0';
6701                PrintType(exp.expType, typeString, false, false);
6702                decl = SpecDeclFromString(typeString, specs, null);
6703
6704                exp.type = castExp;
6705                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6706                exp.cast.typeName = MkTypeName(specs, decl);
6707                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6708                exp.cast.exp.needCast = true;
6709                exp.needTemplateCast = 2;
6710                newExp.needTemplateCast = 2;
6711             }
6712             break;
6713          }
6714       }
6715    }
6716 }
6717 // TODO: The Symbol tree should be reorganized by namespaces
6718 // Name Space:
6719 //    - Tree of all symbols within (stored without namespace)
6720 //    - Tree of sub-namespaces
6721
6722 static Symbol ScanWithNameSpace(BinaryTree tree, const char * nameSpace, const char * name)
6723 {
6724    int nsLen = strlen(nameSpace);
6725    Symbol symbol;
6726    // Start at the name space prefix
6727    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6728    {
6729       char * s = symbol.string;
6730       if(!strncmp(s, nameSpace, nsLen))
6731       {
6732          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6733          int c;
6734          char * namePart;
6735          for(c = strlen(s)-1; c >= 0; c--)
6736             if(s[c] == ':')
6737                break;
6738
6739          namePart = s+c+1;
6740          if(!strcmp(namePart, name))
6741          {
6742             // TODO: Error on ambiguity
6743             return symbol;
6744          }
6745       }
6746       else
6747          break;
6748    }
6749    return null;
6750 }
6751
6752 static Symbol FindWithNameSpace(BinaryTree tree, const char * name)
6753 {
6754    int c;
6755    char nameSpace[1024];
6756    const char * namePart;
6757    bool gotColon = false;
6758
6759    nameSpace[0] = '\0';
6760    for(c = strlen(name)-1; c >= 0; c--)
6761       if(name[c] == ':')
6762       {
6763          gotColon = true;
6764          break;
6765       }
6766
6767    namePart = name+c+1;
6768    while(c >= 0 && name[c] == ':') c--;
6769    if(c >= 0)
6770    {
6771       // Try an exact match first
6772       Symbol symbol = (Symbol)tree.FindString(name);
6773       if(symbol)
6774          return symbol;
6775
6776       // Namespace specified
6777       memcpy(nameSpace, name, c + 1);
6778       nameSpace[c+1] = 0;
6779
6780       return ScanWithNameSpace(tree, nameSpace, namePart);
6781    }
6782    else if(gotColon)
6783    {
6784       // Looking for a global symbol, e.g. ::Sleep()
6785       Symbol symbol = (Symbol)tree.FindString(namePart);
6786       return symbol;
6787    }
6788    else
6789    {
6790       // Name only (no namespace specified)
6791       Symbol symbol = (Symbol)tree.FindString(namePart);
6792       if(symbol)
6793          return symbol;
6794       return ScanWithNameSpace(tree, "", namePart);
6795    }
6796    return null;
6797 }
6798
6799 /*static */Symbol FindSymbol(const char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6800 {
6801 #ifdef _DEBUG
6802    //Time startTime = GetTime();
6803 #endif
6804    // Optimize this later? Do this before/less?
6805    Context ctx;
6806    Symbol symbol = null;
6807
6808    // First, check if the identifier is declared inside the function
6809    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6810
6811    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6812    {
6813       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6814       {
6815          symbol = null;
6816          if(thisNameSpace)
6817          {
6818             char curName[1024];
6819             strcpy(curName, thisNameSpace);
6820             strcat(curName, "::");
6821             strcat(curName, name);
6822             // Try to resolve in current namespace first
6823             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6824          }
6825          if(!symbol)
6826             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6827       }
6828       else
6829          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6830
6831       if(symbol || ctx == endContext) break;
6832    }
6833    if(inCompiler && symbol && ctx == globalContext && symbol.pointerExternal && curExternal && symbol.pointerExternal != curExternal)
6834       curExternal.CreateUniqueEdge(symbol.pointerExternal, symbol.pointerExternal.type == functionExternal);
6835 #ifdef _DEBUG
6836    //findSymbolTotalTime += GetTime() - startTime;
6837 #endif
6838    return symbol;
6839 }
6840
6841 static void GetTypeSpecs(Type type, OldList * specs)
6842 {
6843    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6844    switch(type.kind)
6845    {
6846       case classType:
6847       {
6848          if(type._class.registered)
6849          {
6850             if(!type._class.registered.dataType)
6851                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6852             GetTypeSpecs(type._class.registered.dataType, specs);
6853          }
6854          break;
6855       }
6856       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6857       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6858       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6859       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6860       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6861       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6862       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6863       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6864       case intType:
6865       default:
6866          ListAdd(specs, MkSpecifier(INT)); break;
6867    }
6868 }
6869
6870 static void PrintArraySize(Type arrayType, char * string)
6871 {
6872    char size[256];
6873    size[0] = '\0';
6874    strcat(size, "[");
6875    if(arrayType.enumClass)
6876       strcat(size, arrayType.enumClass.string);
6877    else if(arrayType.arraySizeExp)
6878       PrintExpression(arrayType.arraySizeExp, size);
6879    strcat(size, "]");
6880    strcat(string, size);
6881 }
6882
6883 // WARNING : This function expects a null terminated string since it recursively concatenate...
6884 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6885 {
6886    if(type)
6887    {
6888       if(printConst && type.constant)
6889          strcat(string, "const ");
6890       switch(type.kind)
6891       {
6892          case classType:
6893          {
6894             Symbol c = type._class;
6895             bool isObjectBaseClass = !c || !c.string || !strcmp(c.string, "class");
6896             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6897             //       look into merging with thisclass ?
6898             if(type.classObjectType == typedObject && isObjectBaseClass)
6899                strcat(string, "typed_object");
6900             else if(type.classObjectType == anyObject && isObjectBaseClass)
6901                strcat(string, "any_object");
6902             else
6903             {
6904                if(c && c.string)
6905                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6906             }
6907             if(type.byReference)
6908                strcat(string, " &");
6909             break;
6910          }
6911          case voidType: strcat(string, "void"); break;
6912          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6913          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6914          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6915          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6916          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6917          case _BoolType: strcat(string, "_Bool"); break;
6918          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6919          case floatType: strcat(string, "float"); break;
6920          case doubleType: strcat(string, "double"); break;
6921          case structType:
6922             if(type.enumName)
6923             {
6924                strcat(string, "struct ");
6925                strcat(string, type.enumName);
6926             }
6927             else if(type.typeName)
6928                strcat(string, type.typeName);
6929             else
6930             {
6931                Type member;
6932                strcat(string, "struct { ");
6933                for(member = type.members.first; member; member = member.next)
6934                {
6935                   PrintType(member, string, true, fullName);
6936                   strcat(string,"; ");
6937                }
6938                strcat(string,"}");
6939             }
6940             break;
6941          case unionType:
6942             if(type.enumName)
6943             {
6944                strcat(string, "union ");
6945                strcat(string, type.enumName);
6946             }
6947             else if(type.typeName)
6948                strcat(string, type.typeName);
6949             else
6950             {
6951                strcat(string, "union ");
6952                strcat(string,"(unnamed)");
6953             }
6954             break;
6955          case enumType:
6956             if(type.enumName)
6957             {
6958                strcat(string, "enum ");
6959                strcat(string, type.enumName);
6960             }
6961             else if(type.typeName)
6962                strcat(string, type.typeName);
6963             else
6964                strcat(string, "int"); // "enum");
6965             break;
6966          case ellipsisType:
6967             strcat(string, "...");
6968             break;
6969          case subClassType:
6970             strcat(string, "subclass(");
6971             strcat(string, type._class ? type._class.string : "int");
6972             strcat(string, ")");
6973             break;
6974          case templateType:
6975             strcat(string, type.templateParameter.identifier.string);
6976             break;
6977          case thisClassType:
6978             strcat(string, "thisclass");
6979             break;
6980          case vaListType:
6981             strcat(string, "__builtin_va_list");
6982             break;
6983       }
6984    }
6985 }
6986
6987 static void PrintName(Type type, char * string, bool fullName)
6988 {
6989    if(type.name && type.name[0])
6990    {
6991       if(fullName)
6992          strcat(string, type.name);
6993       else
6994       {
6995          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6996          if(name) name += 2; else name = type.name;
6997          strcat(string, name);
6998       }
6999    }
7000 }
7001
7002 static void PrintAttribs(Type type, char * string)
7003 {
7004    if(type)
7005    {
7006       if(type.dllExport)   strcat(string, "dllexport ");
7007       if(type.attrStdcall) strcat(string, "stdcall ");
7008    }
7009 }
7010
7011 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
7012 {
7013    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7014    {
7015       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
7016          PrintAttribs(type, string);
7017       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
7018          strcat(string, " const");
7019       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
7020       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7021          strcat(string, " (");
7022       if(type.kind == pointerType)
7023       {
7024          if(type.type.kind == functionType || type.type.kind == methodType)
7025             PrintAttribs(type.type, string);
7026       }
7027       if(type.kind == pointerType)
7028       {
7029          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
7030             strcat(string, "*");
7031          else
7032             strcat(string, " *");
7033       }
7034       if(printConst && type.constant && type.kind == pointerType)
7035          strcat(string, " const");
7036    }
7037    else
7038       PrintTypeSpecs(type, string, fullName, printConst);
7039 }
7040
7041 static void PostPrintType(Type type, char * string, bool fullName)
7042 {
7043    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
7044       strcat(string, ")");
7045    if(type.kind == arrayType)
7046       PrintArraySize(type, string);
7047    else if(type.kind == functionType)
7048    {
7049       Type param;
7050       strcat(string, "(");
7051       for(param = type.params.first; param; param = param.next)
7052       {
7053          PrintType(param, string, true, fullName);
7054          if(param.next) strcat(string, ", ");
7055       }
7056       strcat(string, ")");
7057    }
7058    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
7059       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
7060 }
7061
7062 // *****
7063 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
7064 // *****
7065 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
7066 {
7067    PrePrintType(type, string, fullName, null, printConst);
7068
7069    if(type.thisClass || (printName && type.name && type.name[0]))
7070       strcat(string, " ");
7071    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
7072    {
7073       Symbol _class = type.thisClass;
7074       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
7075       {
7076          if(type.classObjectType == classPointer)
7077             strcat(string, "class");
7078          else
7079             strcat(string, type.byReference ? "typed_object&" : "typed_object");
7080       }
7081       else if(_class && _class.string)
7082       {
7083          String s = _class.string;
7084          if(fullName)
7085             strcat(string, s);
7086          else
7087          {
7088             char * name = RSearchString(s, "::", strlen(s), true, false);
7089             if(name) name += 2; else name = s;
7090             strcat(string, name);
7091          }
7092       }
7093       strcat(string, "::");
7094    }
7095
7096    if(printName && type.name)
7097       PrintName(type, string, fullName);
7098    PostPrintType(type, string, fullName);
7099    if(type.bitFieldCount)
7100    {
7101       char count[100];
7102       sprintf(count, ":%d", type.bitFieldCount);
7103       strcat(string, count);
7104    }
7105 }
7106
7107 void PrintType(Type type, char * string, bool printName, bool fullName)
7108 {
7109    _PrintType(type, string, printName, fullName, true);
7110 }
7111
7112 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
7113 {
7114    _PrintType(type, string, printName, fullName, false);
7115 }
7116
7117 static Type FindMember(Type type, char * string)
7118 {
7119    Type memberType;
7120    for(memberType = type.members.first; memberType; memberType = memberType.next)
7121    {
7122       if(!memberType.name)
7123       {
7124          Type subType = FindMember(memberType, string);
7125          if(subType)
7126             return subType;
7127       }
7128       else if(!strcmp(memberType.name, string))
7129          return memberType;
7130    }
7131    return null;
7132 }
7133
7134 Type FindMemberAndOffset(Type type, char * string, uint * offset)
7135 {
7136    Type memberType;
7137    for(memberType = type.members.first; memberType; memberType = memberType.next)
7138    {
7139       if(!memberType.name)
7140       {
7141          Type subType = FindMember(memberType, string);
7142          if(subType)
7143          {
7144             *offset += memberType.offset;
7145             return subType;
7146          }
7147       }
7148       else if(!strcmp(memberType.name, string))
7149       {
7150          *offset += memberType.offset;
7151          return memberType;
7152       }
7153    }
7154    return null;
7155 }
7156
7157 public bool GetParseError() { return parseError; }
7158
7159 Expression ParseExpressionString(char * expression)
7160 {
7161    parseError = false;
7162
7163    fileInput = TempFile { };
7164    fileInput.Write(expression, 1, strlen(expression));
7165    fileInput.Seek(0, start);
7166
7167    echoOn = false;
7168    parsedExpression = null;
7169    resetScanner();
7170    expression_yyparse();
7171    delete fileInput;
7172
7173    return parsedExpression;
7174 }
7175
7176 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7177 {
7178    Identifier id = exp.identifier;
7179    Method method = null;
7180    Property prop = null;
7181    DataMember member = null;
7182    ClassProperty classProp = null;
7183
7184    if(_class && _class.type == enumClass)
7185    {
7186       NamedLink64 value = null;
7187       Class enumClass = eSystem_FindClass(privateModule, "enum");
7188       if(enumClass)
7189       {
7190          Class baseClass;
7191          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7192          {
7193             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7194             for(value = e.values.first; value; value = value.next)
7195             {
7196                if(!strcmp(value.name, id.string))
7197                   break;
7198             }
7199             if(value)
7200             {
7201                exp.isConstant = true;
7202                if(inCompiler || inPreCompiler || inDebugger)
7203                {
7204                   char constant[256];
7205                   FreeExpContents(exp);
7206
7207                   exp.type = constantExp;
7208                   if(!strcmp(baseClass.dataTypeString, "int") || !strcmp(baseClass.dataTypeString, "int64") || !strcmp(baseClass.dataTypeString, "char") || !strcmp(baseClass.dataTypeString, "short"))
7209                      sprintf(constant, FORMAT64D, value.data);
7210                   else
7211                      sprintf(constant, FORMAT64HEX, value.data);
7212                   exp.constant = CopyString(constant);
7213                }
7214                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7215                exp.expType = MkClassType(baseClass.fullName);
7216                break;
7217             }
7218          }
7219       }
7220       if(value)
7221          return true;
7222    }
7223    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7224    {
7225       ProcessMethodType(method);
7226       exp.expType = Type
7227       {
7228          refCount = 1;
7229          kind = methodType;
7230          method = method;
7231          // Crash here?
7232          // TOCHECK: Put it back to what it was...
7233          // methodClass = _class;
7234          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7235       };
7236       //id._class = null;
7237       return true;
7238    }
7239    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7240    {
7241       if(!prop.dataType)
7242          ProcessPropertyType(prop);
7243       exp.expType = prop.dataType;
7244       if(prop.dataType) prop.dataType.refCount++;
7245       return true;
7246    }
7247    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7248    {
7249       if(!member.dataType)
7250          member.dataType = ProcessTypeString(member.dataTypeString, false);
7251       exp.expType = member.dataType;
7252       if(member.dataType) member.dataType.refCount++;
7253       return true;
7254    }
7255    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7256    {
7257       if(!classProp.dataType)
7258          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7259
7260       if(classProp.constant)
7261       {
7262          FreeExpContents(exp);
7263
7264          exp.isConstant = true;
7265          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7266          {
7267             //char constant[256];
7268             exp.type = stringExp;
7269             exp.constant = QMkString((char *)(uintptr)classProp.Get(_class));
7270          }
7271          else
7272          {
7273             char constant[256];
7274             exp.type = constantExp;
7275             sprintf(constant, "%d", (int)classProp.Get(_class));
7276             exp.constant = CopyString(constant);
7277          }
7278       }
7279       else
7280       {
7281          // TO IMPLEMENT...
7282       }
7283
7284       exp.expType = classProp.dataType;
7285       if(classProp.dataType) classProp.dataType.refCount++;
7286       return true;
7287    }
7288    return false;
7289 }
7290
7291 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7292 {
7293    BinaryTree * tree = &nameSpace.functions;
7294    GlobalData data = (GlobalData)tree->FindString(name);
7295    NameSpace * child;
7296    if(!data)
7297    {
7298       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7299       {
7300          data = ScanGlobalData(child, name);
7301          if(data)
7302             break;
7303       }
7304    }
7305    return data;
7306 }
7307
7308 static GlobalData FindGlobalData(char * name)
7309 {
7310    int start = 0, c;
7311    NameSpace * nameSpace;
7312    nameSpace = globalData;
7313    for(c = 0; name[c]; c++)
7314    {
7315       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7316       {
7317          NameSpace * newSpace;
7318          char * spaceName = new char[c - start + 1];
7319          strncpy(spaceName, name + start, c - start);
7320          spaceName[c-start] = '\0';
7321          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7322          delete spaceName;
7323          if(!newSpace)
7324             return null;
7325          nameSpace = newSpace;
7326          if(name[c] == ':') c++;
7327          start = c+1;
7328       }
7329    }
7330    if(c - start)
7331    {
7332       return ScanGlobalData(nameSpace, name + start);
7333    }
7334    return null;
7335 }
7336
7337 static int definedExpStackPos;
7338 static void * definedExpStack[512];
7339
7340 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7341 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7342 {
7343    Expression prev = checkedExp.prev, next = checkedExp.next;
7344
7345    FreeExpContents(checkedExp);
7346    FreeType(checkedExp.expType);
7347    FreeType(checkedExp.destType);
7348
7349    *checkedExp = *newExp;
7350
7351    delete newExp;
7352
7353    checkedExp.prev = prev;
7354    checkedExp.next = next;
7355 }
7356
7357 void ApplyAnyObjectLogic(Expression e)
7358 {
7359    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7360 #ifdef _DEBUG
7361    char debugExpString[4096];
7362    debugExpString[0] = '\0';
7363    PrintExpression(e, debugExpString);
7364 #endif
7365
7366    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7367    {
7368       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7369       //ellipsisDestType = destType;
7370       if(e && e.expType)
7371       {
7372          Type type = e.expType;
7373          Class _class = null;
7374          //Type destType = e.destType;
7375
7376          if(type.kind == classType && type._class && type._class.registered)
7377          {
7378             _class = type._class.registered;
7379          }
7380          else if(type.kind == subClassType)
7381          {
7382             _class = FindClass("ecere::com::Class").registered;
7383          }
7384          else
7385          {
7386             char string[1024] = "";
7387             Symbol classSym;
7388
7389             PrintTypeNoConst(type, string, false, true);
7390             classSym = FindClass(string);
7391             if(classSym) _class = classSym.registered;
7392          }
7393
7394          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...
7395             (!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))) ||
7396             destType.byReference)))
7397          {
7398             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7399             {
7400                Expression checkedExp = e, newExp;
7401
7402                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7403                {
7404                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7405                   {
7406                      if(checkedExp.type == extensionCompoundExp)
7407                      {
7408                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7409                      }
7410                      else
7411                         checkedExp = checkedExp.list->last;
7412                   }
7413                   else if(checkedExp.type == castExp)
7414                      checkedExp = checkedExp.cast.exp;
7415                }
7416
7417                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7418                {
7419                   newExp = checkedExp.op.exp2;
7420                   checkedExp.op.exp2 = null;
7421                   FreeExpContents(checkedExp);
7422
7423                   if(e.expType && e.expType.passAsTemplate)
7424                   {
7425                      char size[100];
7426                      ComputeTypeSize(e.expType);
7427                      sprintf(size, "%d", e.expType.size);   // Potential 32/64 Bootstrap issue
7428                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7429                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7430                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7431                   }
7432
7433                   ReplaceExpContents(checkedExp, newExp);
7434                   e.byReference = true;
7435                }
7436                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7437                {
7438                   Expression checkedExp; //, newExp;
7439
7440                   {
7441                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7442                      bool hasAddress =
7443                         e.type == identifierExp ||
7444                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7445                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7446                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7447                         e.type == indexExp;
7448
7449                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7450                      {
7451                         Context context = PushContext();
7452                         Declarator decl;
7453                         OldList * specs = MkList();
7454                         char typeString[1024];
7455                         Expression newExp { };
7456
7457                         typeString[0] = '\0';
7458                         *newExp = *e;
7459
7460                         //if(e.destType) e.destType.refCount++;
7461                         // if(exp.expType) exp.expType.refCount++;
7462                         newExp.prev = null;
7463                         newExp.next = null;
7464                         newExp.expType = null;
7465
7466                         PrintTypeNoConst(e.expType, typeString, false, true);
7467                         decl = SpecDeclFromString(typeString, specs, null);
7468                         newExp.destType = ProcessType(specs, decl);
7469
7470                         curContext = context;
7471
7472                         // We need a current compound for this
7473                         if(curCompound)
7474                         {
7475                            char name[100];
7476                            OldList * stmts = MkList();
7477                            e.type = extensionCompoundExp;
7478                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7479                            if(!curCompound.compound.declarations)
7480                               curCompound.compound.declarations = MkList();
7481                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7482                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7483                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7484                            e.compound = MkCompoundStmt(null, stmts);
7485                         }
7486                         else
7487                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7488
7489                         /*
7490                         e.compound = MkCompoundStmt(
7491                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7492                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7493
7494                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7495                         */
7496
7497                         {
7498                            Type type = e.destType;
7499                            e.destType = { };
7500                            CopyTypeInto(e.destType, type);
7501                            e.destType.refCount = 1;
7502                            e.destType.classObjectType = none;
7503                            FreeType(type);
7504                         }
7505
7506                         e.compound.compound.context = context;
7507                         PopContext(context);
7508                         curContext = context.parent;
7509                      }
7510                   }
7511
7512                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7513                   checkedExp = e;
7514                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7515                   {
7516                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7517                      {
7518                         if(checkedExp.type == extensionCompoundExp)
7519                         {
7520                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7521                         }
7522                         else
7523                            checkedExp = checkedExp.list->last;
7524                      }
7525                      else if(checkedExp.type == castExp)
7526                         checkedExp = checkedExp.cast.exp;
7527                   }
7528                   {
7529                      Expression operand { };
7530                      operand = *checkedExp;
7531                      checkedExp.Clear();
7532                      checkedExp.destType = ProcessTypeString("void *", false);
7533                      checkedExp.expType = checkedExp.destType;
7534                      checkedExp.destType.refCount++;
7535
7536                      checkedExp.type = opExp;
7537                      checkedExp.op.op = '&';
7538                      checkedExp.op.exp1 = null;
7539                      checkedExp.op.exp2 = operand;
7540
7541                      //newExp = MkExpOp(null, '&', checkedExp);
7542                   }
7543                   //ReplaceExpContents(checkedExp, newExp);
7544                }
7545             }
7546          }
7547       }
7548    }
7549    {
7550       // If expression type is a simple class, make it an address
7551       // FixReference(e, true);
7552    }
7553 //#if 0
7554    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7555       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7556          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7557    {
7558       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"))
7559       {
7560          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7561       }
7562       else
7563       {
7564          Expression thisExp { };
7565
7566          *thisExp = *e;
7567          thisExp.prev = null;
7568          thisExp.next = null;
7569          e.Clear();
7570
7571          e.type = bracketsExp;
7572          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7573          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7574             ((Expression)e.list->first).byReference = true;
7575
7576          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7577          {
7578             e.expType = thisExp.expType;
7579             e.expType.refCount++;
7580          }
7581          else*/
7582          {
7583             e.expType = { };
7584             CopyTypeInto(e.expType, thisExp.expType);
7585             e.expType.byReference = false;
7586             e.expType.refCount = 1;
7587
7588             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7589                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7590             {
7591                e.expType.classObjectType = none;
7592             }
7593          }
7594       }
7595    }
7596 // TOFIX: Try this for a nice IDE crash!
7597 //#endif
7598    // The other way around
7599    else
7600 //#endif
7601    if(destType && e.expType &&
7602          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7603          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7604          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7605    {
7606       if(destType.kind == ellipsisType)
7607       {
7608          Compiler_Error($"Unspecified type\n");
7609       }
7610       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7611       {
7612          bool byReference = e.expType.byReference;
7613          Expression thisExp { };
7614          Declarator decl;
7615          OldList * specs = MkList();
7616          char typeString[1024]; // Watch buffer overruns
7617          Type type;
7618          ClassObjectType backupClassObjectType;
7619          bool backupByReference;
7620
7621          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7622             type = e.expType;
7623          else
7624             type = destType;
7625
7626          backupClassObjectType = type.classObjectType;
7627          backupByReference = type.byReference;
7628
7629          type.classObjectType = none;
7630          type.byReference = false;
7631
7632          typeString[0] = '\0';
7633          PrintType(type, typeString, false, true);
7634          decl = SpecDeclFromString(typeString, specs, null);
7635
7636          type.classObjectType = backupClassObjectType;
7637          type.byReference = backupByReference;
7638
7639          *thisExp = *e;
7640          thisExp.prev = null;
7641          thisExp.next = null;
7642          e.Clear();
7643
7644          if( ( type.kind == classType && type._class && type._class.registered &&
7645                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7646                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7647              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7648              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7649          {
7650             bool passAsTemplate = thisExp.destType.passAsTemplate;
7651             Type t;
7652
7653             destType.refCount++;
7654
7655             e.type = opExp;
7656             e.op.op = '*';
7657             e.op.exp1 = null;
7658             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7659
7660             t = { };
7661             CopyTypeInto(t, thisExp.destType);
7662             t.passAsTemplate = false;
7663             FreeType(thisExp.destType);
7664             thisExp.destType = t;
7665
7666             t = { };
7667             CopyTypeInto(t, destType);
7668             t.passAsTemplate = passAsTemplate;
7669             FreeType(destType);
7670             destType = t;
7671             destType.refCount = 0;
7672
7673             e.expType = { };
7674             CopyTypeInto(e.expType, type);
7675             if(type.passAsTemplate)
7676             {
7677                e.expType.classObjectType = none;
7678                e.expType.passAsTemplate = false;
7679             }
7680             e.expType.byReference = false;
7681             e.expType.refCount = 1;
7682          }
7683          else
7684          {
7685             e.type = castExp;
7686             e.cast.typeName = MkTypeName(specs, decl);
7687             e.cast.exp = thisExp;
7688             e.byReference = true;
7689             e.expType = type;
7690             type.refCount++;
7691          }
7692
7693          if(e.destType)
7694             FreeType(e.destType);
7695
7696          e.destType = destType;
7697          destType.refCount++;
7698       }
7699    }
7700 }
7701
7702 void ApplyLocation(Expression exp, Location loc)
7703 {
7704    exp.loc = loc;
7705    switch(exp.type)
7706    {
7707       case opExp:
7708          if(exp.op.exp1) ApplyLocation(exp.op.exp1, loc);
7709          if(exp.op.exp2) ApplyLocation(exp.op.exp2, loc);
7710          break;
7711       case bracketsExp:
7712          if(exp.list)
7713          {
7714             Expression e;
7715             for(e = exp.list->first; e; e = e.next)
7716                ApplyLocation(e, loc);
7717          }
7718          break;
7719       case indexExp:
7720          if(exp.index.index)
7721          {
7722             Expression e;
7723             for(e = exp.index.index->first; e; e = e.next)
7724                ApplyLocation(e, loc);
7725          }
7726          if(exp.index.exp)
7727             ApplyLocation(exp.index.exp, loc);
7728          break;
7729       case callExp:
7730          if(exp.call.arguments)
7731          {
7732             Expression arg;
7733             for(arg = exp.call.arguments->first; arg; arg = arg.next)
7734                ApplyLocation(arg, loc);
7735          }
7736          if(exp.call.exp)
7737             ApplyLocation(exp.call.exp, loc);
7738          break;
7739       case memberExp:
7740       case pointerExp:
7741          if(exp.member.exp)
7742             ApplyLocation(exp.member.exp, loc);
7743          break;
7744       case castExp:
7745          if(exp.cast.exp)
7746             ApplyLocation(exp.cast.exp, loc);
7747          break;
7748       case conditionExp:
7749          if(exp.cond.exp)
7750          {
7751             Expression e;
7752             for(e = exp.cond.exp->first; e; e = e.next)
7753                ApplyLocation(e, loc);
7754          }
7755          if(exp.cond.cond)
7756             ApplyLocation(exp.cond.cond, loc);
7757          if(exp.cond.elseExp)
7758             ApplyLocation(exp.cond.elseExp, loc);
7759          break;
7760       case vaArgExp:
7761          if(exp.vaArg.exp)
7762             ApplyLocation(exp.vaArg.exp, loc);
7763          break;
7764       default:
7765          break;
7766    }
7767 }
7768
7769 void ProcessExpressionType(Expression exp)
7770 {
7771    bool unresolved = false;
7772    Location oldyylloc = yylloc;
7773    bool notByReference = false;
7774 #ifdef _DEBUG
7775    char debugExpString[4096];
7776    debugExpString[0] = '\0';
7777    PrintExpression(exp, debugExpString);
7778 #endif
7779    if(!exp || exp.expType)
7780       return;
7781
7782    //eSystem_Logf("%s\n", expString);
7783
7784    // Testing this here
7785    yylloc = exp.loc;
7786    switch(exp.type)
7787    {
7788       case identifierExp:
7789       {
7790          Identifier id = exp.identifier;
7791          if(!id || !topContext) return;
7792
7793          // DOING THIS LATER NOW...
7794          if(id._class && id._class.name)
7795          {
7796             id.classSym = id._class.symbol; // FindClass(id._class.name);
7797             /* TODO: Name Space Fix ups
7798             if(!id.classSym)
7799                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7800             */
7801          }
7802
7803          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7804          {
7805             exp.expType = ProcessTypeString("Module", true);
7806             break;
7807          }
7808          else */
7809          if(!strcmp(id.string, "__runtimePlatform"))
7810          {
7811             exp.expType = ProcessTypeString("ecere::com::Platform", true);
7812             break;
7813          }
7814          else if(strstr(id.string, "__ecereClass") == id.string)
7815          {
7816             exp.expType = ProcessTypeString("ecere::com::Class", true);
7817             break;
7818          }
7819          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7820          {
7821             // Added this here as well
7822             ReplaceClassMembers(exp, thisClass);
7823             if(exp.type != identifierExp)
7824             {
7825                ProcessExpressionType(exp);
7826                break;
7827             }
7828
7829             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7830                break;
7831          }
7832          else
7833          {
7834             Symbol symbol = null;
7835             bool findInGlobal = false;
7836             if(!topContext.parent && exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == enumClass)
7837                findInGlobal = true;  // In global context, look at enum values first
7838             else
7839                symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7840
7841             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7842             if(!symbol/* && exp.destType*/)
7843             {
7844                if(exp.destType && CheckExpressionType(exp, exp.destType, false, false))
7845                   break;
7846                else
7847                {
7848                   if(thisClass)
7849                   {
7850                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7851                      if(exp.type != identifierExp)
7852                      {
7853                         ProcessExpressionType(exp);
7854                         break;
7855                      }
7856                   }
7857                   // Static methods called from inside the _class
7858                   else if(currentClass && !id._class)
7859                   {
7860                      if(ResolveIdWithClass(exp, currentClass, true))
7861                         break;
7862                   }
7863                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7864                }
7865             }
7866             if(findInGlobal)
7867                symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7868
7869             // If we manage to resolve this symbol
7870             if(symbol)
7871             {
7872                Type type = symbol.type;
7873                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7874
7875                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7876                {
7877                   Context context = SetupTemplatesContext(_class);
7878                   type = ReplaceThisClassType(_class);
7879                   FinishTemplatesContext(context);
7880                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7881                }
7882
7883                FreeSpecifier(id._class);
7884                id._class = null;
7885                delete id.string;
7886                id.string = CopyString(symbol.string);
7887
7888                id.classSym = null;
7889                exp.expType = type;
7890                if(type)
7891                   type.refCount++;
7892
7893                                                 // Commented this out, it was making non-constant enum parameters seen as constant
7894                                                 // enums should have been resolved by ResolveIdWithClass, changed to constantExp and marked as constant
7895                if(type && (type.kind == enumType /*|| (_class && _class.type == enumClass)*/))
7896                   // Add missing cases here... enum Classes...
7897                   exp.isConstant = true;
7898
7899                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7900                if(symbol.isParam || !strcmp(id.string, "this"))
7901                {
7902                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7903                      exp.byReference = true;
7904
7905                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7906                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7907                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7908                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7909                   {
7910                      Identifier id = exp.identifier;
7911                      exp.type = bracketsExp;
7912                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7913                   }*/
7914                }
7915
7916                if(symbol.isIterator)
7917                {
7918                   if(symbol.isIterator == 3)
7919                   {
7920                      exp.type = bracketsExp;
7921                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7922                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7923                      exp.expType = null;
7924                      ProcessExpressionType(exp);
7925                   }
7926                   else if(symbol.isIterator != 4)
7927                   {
7928                      exp.type = memberExp;
7929                      exp.member.exp = MkExpIdentifier(exp.identifier);
7930                      exp.member.exp.expType = exp.expType;
7931                      /*if(symbol.isIterator == 6)
7932                         exp.member.member = MkIdentifier("key");
7933                      else*/
7934                         exp.member.member = MkIdentifier("data");
7935                      exp.expType = null;
7936                      ProcessExpressionType(exp);
7937                   }
7938                }
7939                break;
7940             }
7941             else
7942             {
7943                DefinedExpression definedExp = null;
7944                if(thisNameSpace && !(id._class && !id._class.name))
7945                {
7946                   char name[1024];
7947                   strcpy(name, thisNameSpace);
7948                   strcat(name, "::");
7949                   strcat(name, id.string);
7950                   definedExp = eSystem_FindDefine(privateModule, name);
7951                }
7952                if(!definedExp)
7953                   definedExp = eSystem_FindDefine(privateModule, id.string);
7954                if(definedExp)
7955                {
7956                   int c;
7957                   for(c = 0; c<definedExpStackPos; c++)
7958                      if(definedExpStack[c] == definedExp)
7959                         break;
7960                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7961                   {
7962                      Location backupYylloc = yylloc;
7963                      File backInput = fileInput;
7964                      definedExpStack[definedExpStackPos++] = definedExp;
7965
7966                      fileInput = TempFile { };
7967                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7968                      fileInput.Seek(0, start);
7969
7970                      echoOn = false;
7971                      parsedExpression = null;
7972                      resetScanner();
7973                      expression_yyparse();
7974                      delete fileInput;
7975                      if(backInput)
7976                         fileInput = backInput;
7977
7978                      yylloc = backupYylloc;
7979
7980                      if(parsedExpression)
7981                      {
7982                         FreeIdentifier(id);
7983                         exp.type = bracketsExp;
7984                         exp.list = MkListOne(parsedExpression);
7985                         ApplyLocation(parsedExpression, yylloc);
7986                         ProcessExpressionType(exp);
7987                         definedExpStackPos--;
7988                         return;
7989                      }
7990                      definedExpStackPos--;
7991                   }
7992                   else
7993                   {
7994                      if(inCompiler)
7995                      {
7996                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7997                      }
7998                   }
7999                }
8000                else
8001                {
8002                   GlobalData data = null;
8003                   if(thisNameSpace && !(id._class && !id._class.name))
8004                   {
8005                      char name[1024];
8006                      strcpy(name, thisNameSpace);
8007                      strcat(name, "::");
8008                      strcat(name, id.string);
8009                      data = FindGlobalData(name);
8010                   }
8011                   if(!data)
8012                      data = FindGlobalData(id.string);
8013                   if(data)
8014                   {
8015                      DeclareGlobalData(curExternal, data);
8016                      exp.expType = data.dataType;
8017                      if(data.dataType) data.dataType.refCount++;
8018
8019                      delete id.string;
8020                      id.string = CopyString(data.fullName);
8021                      FreeSpecifier(id._class);
8022                      id._class = null;
8023
8024                      break;
8025                   }
8026                   else
8027                   {
8028                      GlobalFunction function = null;
8029                      if(thisNameSpace && !(id._class && !id._class.name))
8030                      {
8031                         char name[1024];
8032                         strcpy(name, thisNameSpace);
8033                         strcat(name, "::");
8034                         strcat(name, id.string);
8035                         function = eSystem_FindFunction(privateModule, name);
8036                      }
8037                      if(!function)
8038                         function = eSystem_FindFunction(privateModule, id.string);
8039                      if(function)
8040                      {
8041                         char name[1024];
8042                         delete id.string;
8043                         id.string = CopyString(function.name);
8044                         name[0] = 0;
8045
8046                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
8047                            strcpy(name, "__ecereFunction_");
8048                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
8049                         if(DeclareFunction(curExternal, function, name))
8050                         {
8051                            delete id.string;
8052                            id.string = CopyString(name);
8053                         }
8054                         exp.expType = function.dataType;
8055                         if(function.dataType) function.dataType.refCount++;
8056
8057                         FreeSpecifier(id._class);
8058                         id._class = null;
8059
8060                         break;
8061                      }
8062                   }
8063                }
8064             }
8065          }
8066          unresolved = true;
8067          break;
8068       }
8069       case instanceExp:
8070       {
8071          // Class _class;
8072          // Symbol classSym;
8073
8074          if(!exp.instance._class)
8075          {
8076             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
8077             {
8078                exp.instance._class = MkSpecifierName(exp.destType._class.string);
8079             }
8080          }
8081
8082          //classSym = FindClass(exp.instance._class.fullName);
8083          //_class = classSym ? classSym.registered : null;
8084
8085          ProcessInstantiationType(exp.instance);
8086
8087          exp.isConstant = exp.instance.isConstant;
8088
8089          /*
8090          if(_class.type == unitClass && _class.base.type != systemClass)
8091          {
8092             {
8093                Type destType = exp.destType;
8094
8095                exp.destType = MkClassType(_class.base.fullName);
8096                exp.expType = MkClassType(_class.fullName);
8097                CheckExpressionType(exp, exp.destType, true);
8098
8099                exp.destType = destType;
8100             }
8101             exp.expType = MkClassType(_class.fullName);
8102          }
8103          else*/
8104          if(exp.instance._class)
8105          {
8106             exp.expType = MkClassType(exp.instance._class.name);
8107             /*if(exp.expType._class && exp.expType._class.registered &&
8108                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
8109                exp.expType.byReference = true;*/
8110          }
8111          break;
8112       }
8113       case constantExp:
8114       {
8115          if(!exp.expType)
8116          {
8117             char * constant = exp.constant;
8118             Type type
8119             {
8120                refCount = 1;
8121                constant = true;
8122             };
8123             exp.expType = type;
8124
8125             if(constant[0] == '\'')
8126             {
8127                if((int)((byte *)constant)[1] > 127)
8128                {
8129                   int nb;
8130                   unichar ch = UTF8GetChar(constant + 1, &nb);
8131                   if(nb < 2) ch = constant[1];
8132                   delete constant;
8133                   exp.constant = PrintUInt(ch);
8134                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
8135                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
8136                   type._class = FindClass("unichar");
8137
8138                   type.isSigned = false;
8139                }
8140                else
8141                {
8142                   type.kind = charType;
8143                   type.isSigned = true;
8144                }
8145             }
8146             else
8147             {
8148                char * dot = strchr(constant, '.');
8149                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
8150                char * exponent;
8151                if(isHex)
8152                {
8153                   exponent = strchr(constant, 'p');
8154                   if(!exponent) exponent = strchr(constant, 'P');
8155                }
8156                else
8157                {
8158                   exponent = strchr(constant, 'e');
8159                   if(!exponent) exponent = strchr(constant, 'E');
8160                }
8161
8162                if(dot || exponent)
8163                {
8164                   if(strchr(constant, 'f') || strchr(constant, 'F'))
8165                      type.kind = floatType;
8166                   else
8167                      type.kind = doubleType;
8168                   type.isSigned = true;
8169                }
8170                else
8171                {
8172                   bool isSigned = constant[0] == '-';
8173                   char * endP = null;
8174                   int64 i64 = strtoll(constant, &endP, 0);
8175                   uint64 ui64 = strtoull(constant, &endP, 0);
8176                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll") || !strcmp(endP, "LLU") || !strcmp(endP, "llu") || !strcmp(endP, "ull") || !strcmp(endP, "ULL"));
8177                   bool forceUnsigned = endP && (!strcmp(endP, "U") || !strcmp(endP, "u") || !strcmp(endP, "LLU") || !strcmp(endP, "llu") || !strcmp(endP, "ull") || !strcmp(endP, "ULL"));
8178                   if(isSigned)
8179                   {
8180                      if(i64 < MININT)
8181                         is64Bit = true;
8182                   }
8183                   else
8184                   {
8185                      if(ui64 > MAXINT)
8186                      {
8187                         if(ui64 > MAXDWORD)
8188                         {
8189                            is64Bit = true;
8190                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
8191                               isSigned = true;
8192                         }
8193                      }
8194                      else if(constant[0] != '0' || !constant[1])
8195                         isSigned = true;
8196                   }
8197                   if(forceUnsigned)
8198                      isSigned = false;
8199                   type.kind = is64Bit ? int64Type : intType;
8200                   type.isSigned = isSigned;
8201                }
8202             }
8203             exp.isConstant = true;
8204             if(exp.destType && exp.destType.kind == doubleType)
8205                type.kind = doubleType;
8206             else if(exp.destType && exp.destType.kind == floatType)
8207                type.kind = floatType;
8208             else if(exp.destType && exp.destType.kind == int64Type)
8209                type.kind = int64Type;
8210          }
8211          break;
8212       }
8213       case stringExp:
8214       {
8215          exp.isConstant = true;      // Why wasn't this constant?
8216          exp.expType = Type
8217          {
8218             refCount = 1;
8219             kind = pointerType;
8220             type = Type
8221             {
8222                refCount = 1;
8223                kind = exp.wideString ? shortType : charType;
8224                constant = true;
8225                isSigned = exp.wideString ? false : true;
8226             }
8227          };
8228          break;
8229       }
8230       case newExp:
8231       case new0Exp:
8232          ProcessExpressionType(exp._new.size);
8233          exp.expType = Type
8234          {
8235             refCount = 1;
8236             kind = pointerType;
8237             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
8238          };
8239          DeclareType(curExternal, exp.expType.type, true, false);
8240          break;
8241       case renewExp:
8242       case renew0Exp:
8243          ProcessExpressionType(exp._renew.size);
8244          ProcessExpressionType(exp._renew.exp);
8245          exp.expType = Type
8246          {
8247             refCount = 1;
8248             kind = pointerType;
8249             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
8250          };
8251          DeclareType(curExternal, exp.expType.type, true, false);
8252          break;
8253       case opExp:
8254       {
8255          bool assign = false, boolResult = false, boolOps = false;
8256          Type type1 = null, type2 = null;
8257          bool useDestType = false, useSideType = false;
8258          Location oldyylloc = yylloc;
8259          bool useSideUnit = false;
8260          Class destClass = (exp.destType && exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
8261
8262          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
8263          Type dummy
8264          {
8265             count = 1;
8266             refCount = 1;
8267          };
8268
8269          switch(exp.op.op)
8270          {
8271             // Assignment Operators
8272             case '=':
8273             case MUL_ASSIGN:
8274             case DIV_ASSIGN:
8275             case MOD_ASSIGN:
8276             case ADD_ASSIGN:
8277             case SUB_ASSIGN:
8278             case LEFT_ASSIGN:
8279             case RIGHT_ASSIGN:
8280             case AND_ASSIGN:
8281             case XOR_ASSIGN:
8282             case OR_ASSIGN:
8283                assign = true;
8284                break;
8285             // boolean Operators
8286             case '!':
8287                // Expect boolean operators
8288                //boolOps = true;
8289                //boolResult = true;
8290                break;
8291             case AND_OP:
8292             case OR_OP:
8293                // Expect boolean operands
8294                boolOps = true;
8295                boolResult = true;
8296                break;
8297             // Comparisons
8298             case EQ_OP:
8299             case '<':
8300             case '>':
8301             case LE_OP:
8302             case GE_OP:
8303             case NE_OP:
8304                // Gives boolean result
8305                boolResult = true;
8306                useSideType = true;
8307                break;
8308             case '+':
8309             case '-':
8310                useSideUnit = true;
8311                useSideType = true;
8312                useDestType = true;
8313                break;
8314
8315             case LEFT_OP:
8316             case RIGHT_OP:
8317                // useSideType = true;
8318                // useDestType = true;
8319                break;
8320
8321             case '|':
8322             case '^':
8323                useSideType = true;
8324                useDestType = true;
8325                break;
8326
8327             case '/':
8328             case '%':
8329                useSideType = true;
8330                useDestType = true;
8331                break;
8332             case '&':
8333             case '*':
8334                if(exp.op.exp1)
8335                {
8336                   // For & operator, useDestType nicely ensures the result will fit in a bool (TODO: Fix for generic enum)
8337                   useSideType = true;
8338                   useDestType = true;
8339                }
8340                break;
8341
8342             /*// Implement speed etc.
8343             case '*':
8344             case '/':
8345                break;
8346             */
8347          }
8348          if(exp.op.op == '&')
8349          {
8350             // Added this here earlier for Iterator address as key
8351             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8352             {
8353                Identifier id = exp.op.exp2.identifier;
8354                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8355                if(symbol && symbol.isIterator == 2)
8356                {
8357                   exp.type = memberExp;
8358                   exp.member.exp = exp.op.exp2;
8359                   exp.member.member = MkIdentifier("key");
8360                   exp.expType = null;
8361                   exp.op.exp2.expType = symbol.type;
8362                   symbol.type.refCount++;
8363                   ProcessExpressionType(exp);
8364                   FreeType(dummy);
8365                   break;
8366                }
8367                // exp.op.exp2.usage.usageRef = true;
8368             }
8369          }
8370
8371          //dummy.kind = TypeDummy;
8372          if(exp.op.exp1)
8373          {
8374             // Added this check here to use the dest type only for units derived from the base unit
8375             // So that untyped units will use the side unit as opposed to the untyped destination unit
8376             // This fixes (#771) sin(Degrees { 5 } + 5) to be equivalent to sin(Degrees { 10 }), since sin expects a generic Angle
8377             if(exp.op.exp2 && useSideUnit && useDestType && destClass && destClass.type == unitClass && destClass.base.type != unitClass)
8378                useDestType = false;
8379
8380             if(destClass && useDestType &&
8381               ((destClass.type == unitClass && useSideUnit) || destClass.type == enumClass || destClass.type == bitClass))
8382
8383               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8384             {
8385                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8386                exp.op.exp1.destType = exp.destType;
8387                exp.op.exp1.opDestType = true;
8388                if(exp.destType)
8389                   exp.destType.refCount++;
8390             }
8391             else if(!assign)
8392             {
8393                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8394                exp.op.exp1.destType = dummy;
8395                dummy.refCount++;
8396             }
8397
8398             // TESTING THIS HERE...
8399             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8400                ProcessExpressionType(exp.op.exp1);
8401             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8402
8403             exp.op.exp1.opDestType = false;
8404
8405             // Fix for unit and ++ / --
8406             if(!exp.op.exp2 && (exp.op.op == INC_OP || exp.op.op == DEC_OP) && exp.op.exp1.expType && exp.op.exp1.expType.kind == classType &&
8407                exp.op.exp1.expType._class && exp.op.exp1.expType._class.registered && exp.op.exp1.expType._class.registered.type == unitClass)
8408             {
8409                exp.op.exp2 = MkExpConstant("1");
8410                exp.op.op = exp.op.op == INC_OP ? ADD_ASSIGN : SUB_ASSIGN;
8411                assign = true;
8412             }
8413
8414             if(exp.op.exp1.destType == dummy)
8415             {
8416                FreeType(dummy);
8417                exp.op.exp1.destType = null;
8418             }
8419
8420             if(exp.op.exp2)
8421             {
8422                if(!assign && exp.op.exp1.expType && (exp.op.exp1.expType.kind == charType || exp.op.exp1.expType.kind == shortType))
8423                {
8424                   Type type { kind = intType, isSigned = true, refCount = 1, signedBeforePromotion = exp.op.exp1.expType.isSigned, bitMemberSize = exp.op.exp1.expType.bitMemberSize, promotedFrom = exp.op.exp1.expType.kind };
8425                   FreeType(exp.op.exp1.expType);
8426                   exp.op.exp1.expType = type;
8427                }
8428             }
8429
8430             type1 = exp.op.exp1.expType;
8431          }
8432
8433          if(exp.op.exp2)
8434          {
8435             char expString[10240];
8436             expString[0] = '\0';
8437             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8438             {
8439                if(exp.op.exp1)
8440                {
8441                   exp.op.exp2.destType = exp.op.exp1.expType;
8442                   if(exp.op.exp1.expType)
8443                      exp.op.exp1.expType.refCount++;
8444                }
8445                else
8446                {
8447                   exp.op.exp2.destType = exp.destType;
8448                   if(!exp.op.exp1 || (exp.op.op != '&' && exp.op.op != '^'))
8449                      exp.op.exp2.opDestType = true;
8450                   if(exp.destType)
8451                      exp.destType.refCount++;
8452                }
8453
8454                if(type1) type1.refCount++;
8455                exp.expType = type1;
8456             }
8457             else if(assign)
8458             {
8459                if(inCompiler)
8460                   PrintExpression(exp.op.exp2, expString);
8461
8462                if(type1 && type1.kind == pointerType)
8463                {
8464                   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 ||
8465                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8466                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8467                   else if(exp.op.op == '=')
8468                   {
8469                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8470                      exp.op.exp2.destType = type1;
8471                      if(type1)
8472                         type1.refCount++;
8473                   }
8474                }
8475                else
8476                {
8477                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8478                   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/* ||
8479                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8480                   else
8481                   {
8482                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8483                      exp.op.exp2.destType = type1;
8484                      if(type1)
8485                         type1.refCount++;
8486                   }
8487                }
8488                if(type1) type1.refCount++;
8489                exp.expType = type1;
8490             }
8491             else if(destClass &&
8492                   ((destClass.type == unitClass && useDestType && useSideUnit) ||
8493                   (destClass.type == enumClass && useDestType)))
8494             {
8495                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8496                exp.op.exp2.destType = exp.destType;
8497                if(exp.op.op != '&' && exp.op.op != '^')
8498                   exp.op.exp2.opDestType = true;
8499                if(exp.destType)
8500                   exp.destType.refCount++;
8501             }
8502             else
8503             {
8504                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8505                exp.op.exp2.destType = dummy;
8506                dummy.refCount++;
8507             }
8508
8509             // TESTING THIS HERE... (DANGEROUS)
8510             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8511                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8512             {
8513                FreeType(exp.op.exp2.destType);
8514                exp.op.exp2.destType = type1;
8515                type1.refCount++;
8516             }
8517             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8518             // Cannot lose the cast on a sizeof
8519             if(exp.op.op == SIZEOF)
8520             {
8521                Expression e = exp.op.exp2;
8522                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8523                {
8524                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8525                   {
8526                      if(e.type == extensionCompoundExp)
8527                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8528                      else
8529                         e = e.list->last;
8530                   }
8531                }
8532                if(e.type == castExp && e.cast.exp)
8533                   e.cast.exp.needCast = true;
8534             }
8535             ProcessExpressionType(exp.op.exp2);
8536             exp.op.exp2.opDestType = false;
8537             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8538
8539             if(!assign && (exp.op.exp1 || exp.op.op == '~'))
8540             {
8541                if(exp.op.exp2.expType && (exp.op.exp2.expType.kind == charType || exp.op.exp2.expType.kind == shortType))
8542                {
8543                   Type type { kind = intType, isSigned = true, refCount = 1, signedBeforePromotion = exp.op.exp2.expType.isSigned, bitMemberSize = exp.op.exp2.expType.bitMemberSize, promotedFrom = exp.op.exp2.expType.kind };
8544                   FreeType(exp.op.exp2.expType);
8545                   exp.op.exp2.expType = type;
8546                }
8547             }
8548
8549             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8550             {
8551                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)
8552                {
8553                   if(exp.op.op != '=' && type1.type.kind == voidType)
8554                      Compiler_Error($"void *: unknown size\n");
8555                }
8556                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||
8557                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8558                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8559                               exp.op.exp2.expType._class.registered.type == structClass ||
8560                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8561                {
8562                   if(exp.op.op == ADD_ASSIGN)
8563                      Compiler_Error($"cannot add two pointers\n");
8564                }
8565                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8566                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8567                {
8568                   if(exp.op.op == ADD_ASSIGN)
8569                      Compiler_Error($"cannot add two pointers\n");
8570                }
8571                else if(inCompiler)
8572                {
8573                   char type1String[1024];
8574                   char type2String[1024];
8575                   type1String[0] = '\0';
8576                   type2String[0] = '\0';
8577
8578                   PrintType(exp.op.exp2.expType, type1String, false, true);
8579                   PrintType(type1, type2String, false, true);
8580                   ChangeCh(expString, '\n', ' ');
8581                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8582                }
8583             }
8584
8585             if(exp.op.exp2.destType == dummy)
8586             {
8587                FreeType(dummy);
8588                exp.op.exp2.destType = null;
8589             }
8590
8591             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8592             {
8593                type2 = { };
8594                type2.refCount = 1;
8595                CopyTypeInto(type2, exp.op.exp2.expType);
8596                type2.isSigned = true;
8597             }
8598             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8599             {
8600                type2 = { kind = intType };
8601                type2.refCount = 1;
8602                type2.isSigned = true;
8603             }
8604             else
8605             {
8606                type2 = exp.op.exp2.expType;
8607                if(type2) type2.refCount++;
8608             }
8609          }
8610
8611          dummy.kind = voidType;
8612
8613          if(exp.op.op == SIZEOF)
8614          {
8615             exp.expType = Type
8616             {
8617                refCount = 1;
8618                kind = intSizeType;
8619             };
8620             exp.isConstant = true;
8621          }
8622          // Get type of dereferenced pointer
8623          else if(exp.op.op == '*' && !exp.op.exp1)
8624          {
8625             exp.expType = Dereference(type2);
8626             if(type2 && type2.kind == classType)
8627                notByReference = true;
8628          }
8629          else if(exp.op.op == '&' && !exp.op.exp1)
8630             exp.expType = Reference(type2);
8631          else if(exp.op.op == LEFT_OP || exp.op.op == RIGHT_OP)
8632          {
8633             if(exp.op.exp1.expType)
8634             {
8635                exp.expType = exp.op.exp1.expType;
8636                exp.expType.refCount++;
8637             }
8638          }
8639          else if(!assign)
8640          {
8641             if(boolOps)
8642             {
8643                if(exp.op.exp1)
8644                {
8645                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8646                   exp.op.exp1.destType = MkClassType("bool");
8647                   exp.op.exp1.destType.truth = true;
8648                   if(!exp.op.exp1.expType)
8649                      ProcessExpressionType(exp.op.exp1);
8650                   else
8651                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8652                   FreeType(exp.op.exp1.expType);
8653                   exp.op.exp1.expType = MkClassType("bool");
8654                   exp.op.exp1.expType.truth = true;
8655                }
8656                if(exp.op.exp2)
8657                {
8658                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8659                   exp.op.exp2.destType = MkClassType("bool");
8660                   exp.op.exp2.destType.truth = true;
8661                   if(!exp.op.exp2.expType)
8662                      ProcessExpressionType(exp.op.exp2);
8663                   else
8664                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8665                   FreeType(exp.op.exp2.expType);
8666                   exp.op.exp2.expType = MkClassType("bool");
8667                   exp.op.exp2.expType.truth = true;
8668                }
8669             }
8670             else if(exp.op.exp1 && exp.op.exp2 &&
8671                ((useSideType /*&&
8672                      (useSideUnit ||
8673                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8674                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8675                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8676                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8677             {
8678                if(type1 && type2 &&
8679                   // If either both are class or both are not class
8680                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8681                {
8682                   // Added this check for enum subtraction to result in an int type:
8683                   if(exp.op.op == '-' &&
8684                      ((type1.kind == classType && type1._class.registered && type1._class.registered.type == enumClass) ||
8685                       (type2.kind == classType && type2._class.registered && type2._class.registered.type == enumClass)) )
8686                   {
8687                      Type intType;
8688                      if(!type1._class.registered.dataType)
8689                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8690                      if(!type2._class.registered.dataType)
8691                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8692
8693                      intType = ProcessTypeString(
8694                         (type1._class.registered.dataType.kind == int64Type || type2._class.registered.dataType.kind == int64Type) ? "int64" : "int", false);
8695
8696                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8697                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8698                      exp.op.exp1.destType = intType;
8699                      exp.op.exp2.destType = intType;
8700                      intType.refCount++;
8701                   }
8702                   else
8703                   {
8704                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8705                      exp.op.exp2.destType = type1;
8706                      type1.refCount++;
8707                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8708                      exp.op.exp1.destType = type2;
8709                      type2.refCount++;
8710                   }
8711
8712                   // Warning here for adding Radians + Degrees with no destination type
8713                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8714                      type1._class.registered && type1._class.registered.type == unitClass &&
8715                      type2._class.registered && type2._class.registered.type == unitClass &&
8716                      type1._class.registered != type2._class.registered)
8717                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8718                         type1._class.string, type2._class.string, type1._class.string);
8719
8720                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8721                   {
8722                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8723                      if(argExp)
8724                      {
8725                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8726
8727                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8728                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8729                            exp.op.exp1)));
8730
8731                         ProcessExpressionType(exp.op.exp1);
8732
8733                         if(type2.kind != pointerType)
8734                         {
8735                            ProcessExpressionType(classExp);
8736
8737                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*', MkExpMember(classExp, MkIdentifier("typeSize")) )));
8738
8739                            if(!exp.op.exp2.expType)
8740                            {
8741                               if(type2)
8742                                  FreeType(type2);
8743                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8744                               type2.refCount++;
8745                            }
8746
8747                            ProcessExpressionType(exp.op.exp2);
8748                         }
8749                      }
8750                   }
8751
8752                   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)))
8753                   {
8754                      if(type1.kind != classType && type1.type.kind == voidType)
8755                         Compiler_Error($"void *: unknown size\n");
8756                      exp.expType = type1;
8757                      if(type1) type1.refCount++;
8758                   }
8759                   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)))
8760                   {
8761                      if(type2.kind != classType && type2.type.kind == voidType)
8762                         Compiler_Error($"void *: unknown size\n");
8763                      exp.expType = type2;
8764                      if(type2) type2.refCount++;
8765                   }
8766                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8767                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8768                   {
8769                      Compiler_Warning($"different levels of indirection\n");
8770                   }
8771                   else
8772                   {
8773                      bool success = false;
8774                      if(type1.kind == pointerType && type2.kind == pointerType)
8775                      {
8776                         if(exp.op.op == '+')
8777                            Compiler_Error($"cannot add two pointers\n");
8778                         else if(exp.op.op == '-')
8779                         {
8780                            // Pointer Subtraction gives integer
8781                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false, false))
8782                            {
8783                               exp.expType = Type
8784                               {
8785                                  kind = intType;
8786                                  refCount = 1;
8787                               };
8788                               success = true;
8789
8790                               if(type1.type.kind == templateType)
8791                               {
8792                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8793                                  if(argExp)
8794                                  {
8795                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8796
8797                                     ProcessExpressionType(classExp);
8798
8799                                     exp.type = bracketsExp;
8800                                     exp.list = MkListOne(MkExpOp(
8801                                        MkExpBrackets(MkListOne(MkExpOp(
8802                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8803                                              , exp.op.op,
8804                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8805                                              MkExpMember(classExp, MkIdentifier("typeSize"))));
8806
8807                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8808                                     FreeType(dummy);
8809                                     return;
8810                                  }
8811                               }
8812                            }
8813                         }
8814                      }
8815
8816                      if(!success && exp.op.exp1.type == constantExp)
8817                      {
8818                         // If first expression is constant, try to match that first
8819                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8820                         {
8821                            if(exp.expType) FreeType(exp.expType);
8822                            exp.expType = exp.op.exp1.destType;
8823                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8824                            success = true;
8825                         }
8826                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8827                         {
8828                            if(exp.expType) FreeType(exp.expType);
8829                            exp.expType = exp.op.exp2.destType;
8830                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8831                            success = true;
8832                         }
8833                      }
8834                      else if(!success)
8835                      {
8836                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
8837                         {
8838                            if(exp.expType) FreeType(exp.expType);
8839                            exp.expType = exp.op.exp2.destType;
8840                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8841                            success = true;
8842                         }
8843                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
8844                         {
8845                            if(exp.expType) FreeType(exp.expType);
8846                            exp.expType = exp.op.exp1.destType;
8847                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8848                            success = true;
8849                         }
8850                      }
8851                      if(!success)
8852                      {
8853                         char expString1[10240];
8854                         char expString2[10240];
8855                         char type1[1024];
8856                         char type2[1024];
8857                         expString1[0] = '\0';
8858                         expString2[0] = '\0';
8859                         type1[0] = '\0';
8860                         type2[0] = '\0';
8861                         if(inCompiler)
8862                         {
8863                            PrintExpression(exp.op.exp1, expString1);
8864                            ChangeCh(expString1, '\n', ' ');
8865                            PrintExpression(exp.op.exp2, expString2);
8866                            ChangeCh(expString2, '\n', ' ');
8867                            PrintType(exp.op.exp1.expType, type1, false, true);
8868                            PrintType(exp.op.exp2.expType, type2, false, true);
8869                         }
8870
8871                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8872                      }
8873                   }
8874                }
8875                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8876                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8877                {
8878                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8879                   // Convert e.g. / 4 into / 4.0
8880                   exp.op.exp1.destType = type2._class.registered.dataType;
8881                   if(type2._class.registered.dataType)
8882                      type2._class.registered.dataType.refCount++;
8883                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8884                   exp.expType = type2;
8885                   if(type2) type2.refCount++;
8886                }
8887                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8888                {
8889                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8890                   // Convert e.g. / 4 into / 4.0
8891                   exp.op.exp2.destType = type1._class.registered.dataType;
8892                   if(type1._class.registered.dataType)
8893                      type1._class.registered.dataType.refCount++;
8894                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8895                   exp.expType = type1;
8896                   if(type1) type1.refCount++;
8897                }
8898                else if(type1)
8899                {
8900                   bool valid = false;
8901
8902                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8903                   {
8904                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8905
8906                      if(!type1._class.registered.dataType)
8907                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8908                      exp.op.exp2.destType = type1._class.registered.dataType;
8909                      exp.op.exp2.destType.refCount++;
8910
8911                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
8912                      if(type2)
8913                         FreeType(type2);
8914                      type2 = exp.op.exp2.destType;
8915                      if(type2) type2.refCount++;
8916
8917                      exp.expType = type2;
8918                      type2.refCount++;
8919                   }
8920
8921                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8922                   {
8923                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8924
8925                      if(!type2._class.registered.dataType)
8926                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8927                      exp.op.exp1.destType = type2._class.registered.dataType;
8928                      exp.op.exp1.destType.refCount++;
8929
8930                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
8931                      type1 = exp.op.exp1.destType;
8932                      exp.expType = type1;
8933                      type1.refCount++;
8934                   }
8935
8936                   // TESTING THIS NEW CODE
8937                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<' || exp.op.op == GE_OP || exp.op.op == LE_OP)
8938                   {
8939                      bool op1IsEnum = type1 && type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass;
8940                      bool op2IsEnum = type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass;
8941                      if(exp.op.op == '*' || exp.op.op == '/' || exp.op.op == '-' || exp.op.op == '|' || exp.op.op == '^')
8942                      {
8943                         // Convert the enum to an int instead for these operators
8944                         if(op1IsEnum && exp.op.exp2.expType)
8945                         {
8946                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8947                            {
8948                               if(exp.expType) FreeType(exp.expType);
8949                               exp.expType = exp.op.exp2.expType;
8950                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8951                               valid = true;
8952                            }
8953                         }
8954                         else if(op2IsEnum && exp.op.exp1.expType)
8955                         {
8956                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8957                            {
8958                               if(exp.expType) FreeType(exp.expType);
8959                               exp.expType = exp.op.exp1.expType;
8960                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8961                               valid = true;
8962                            }
8963                         }
8964                      }
8965                      else
8966                      {
8967                         if(op1IsEnum && exp.op.exp2.expType)
8968                         {
8969                            if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false, false))
8970                            {
8971                               if(exp.expType) FreeType(exp.expType);
8972                               exp.expType = exp.op.exp1.expType;
8973                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8974                               valid = true;
8975                            }
8976                         }
8977                         else if(op2IsEnum && exp.op.exp1.expType)
8978                         {
8979                            if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false, false))
8980                            {
8981                               if(exp.expType) FreeType(exp.expType);
8982                               exp.expType = exp.op.exp2.expType;
8983                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8984                               valid = true;
8985                            }
8986                         }
8987                      }
8988                   }
8989
8990                   if(!valid)
8991                   {
8992                      // Added this first part of the if here to handle  5 + Degrees { 5 } with either a base unit dest or not a unit dest type
8993                      if(type2 && type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass &&
8994                         (type1.kind != classType || !type1._class || !type1._class.registered || type1._class.registered.type != unitClass))
8995                      {
8996                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8997                         exp.op.exp1.destType = type2;
8998                         type2.refCount++;
8999
9000                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9001                         {
9002                            if(exp.expType) FreeType(exp.expType);
9003                            exp.expType = exp.op.exp1.destType;
9004                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9005                         }
9006                      }
9007                      else
9008                      {
9009                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9010                         exp.op.exp2.destType = type1;
9011                         type1.refCount++;
9012
9013                      /*
9014                      // Maybe this was meant to be an enum...
9015                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9016                      {
9017                         Type oldType = exp.op.exp2.expType;
9018                         exp.op.exp2.expType = null;
9019                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
9020                            FreeType(oldType);
9021                         else
9022                            exp.op.exp2.expType = oldType;
9023                      }
9024                      */
9025
9026                      /*
9027                      // TESTING THIS HERE... LATEST ADDITION
9028                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9029                      {
9030                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9031                         exp.op.exp2.destType = type2._class.registered.dataType;
9032                         if(type2._class.registered.dataType)
9033                            type2._class.registered.dataType.refCount++;
9034                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
9035
9036                         //exp.expType = type2._class.registered.dataType; //type2;
9037                         //if(type2) type2.refCount++;
9038                      }
9039
9040                      // TESTING THIS HERE... LATEST ADDITION
9041                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9042                      {
9043                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9044                         exp.op.exp1.destType = type1._class.registered.dataType;
9045                         if(type1._class.registered.dataType)
9046                            type1._class.registered.dataType.refCount++;
9047                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
9048                         exp.expType = type1._class.registered.dataType; //type1;
9049                         if(type1) type1.refCount++;
9050                      }
9051                      */
9052
9053                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false))
9054                         {
9055                            if(exp.expType) FreeType(exp.expType);
9056                            exp.expType = exp.op.exp2.destType;
9057                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
9058                         }
9059                         else if(type1 && type2)
9060                         {
9061                            char expString1[10240];
9062                            char expString2[10240];
9063                            char type1String[1024];
9064                            char type2String[1024];
9065                            expString1[0] = '\0';
9066                            expString2[0] = '\0';
9067                            type1String[0] = '\0';
9068                            type2String[0] = '\0';
9069                            if(inCompiler)
9070                            {
9071                               PrintExpression(exp.op.exp1, expString1);
9072                               ChangeCh(expString1, '\n', ' ');
9073                               PrintExpression(exp.op.exp2, expString2);
9074                               ChangeCh(expString2, '\n', ' ');
9075                               PrintType(exp.op.exp1.expType, type1String, false, true);
9076                               PrintType(exp.op.exp2.expType, type2String, false, true);
9077                            }
9078
9079                            Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
9080
9081                            if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
9082                            {
9083                               exp.expType = exp.op.exp1.expType;
9084                               if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
9085                            }
9086                            else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9087                            {
9088                               exp.expType = exp.op.exp2.expType;
9089                               if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
9090                            }
9091                         }
9092                      }
9093                   }
9094                }
9095                else if(type2)
9096                {
9097                   // Maybe this was meant to be an enum...
9098                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
9099                   {
9100                      Type oldType = exp.op.exp1.expType;
9101                      exp.op.exp1.expType = null;
9102                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9103                         FreeType(oldType);
9104                      else
9105                         exp.op.exp1.expType = oldType;
9106                   }
9107
9108                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9109                   exp.op.exp1.destType = type2;
9110                   type2.refCount++;
9111                   /*
9112                   // TESTING THIS HERE... LATEST ADDITION
9113                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
9114                   {
9115                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9116                      exp.op.exp1.destType = type1._class.registered.dataType;
9117                      if(type1._class.registered.dataType)
9118                         type1._class.registered.dataType.refCount++;
9119                   }
9120
9121                   // TESTING THIS HERE... LATEST ADDITION
9122                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
9123                   {
9124                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9125                      exp.op.exp2.destType = type2._class.registered.dataType;
9126                      if(type2._class.registered.dataType)
9127                         type2._class.registered.dataType.refCount++;
9128                   }
9129                   */
9130
9131                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false))
9132                   {
9133                      if(exp.expType) FreeType(exp.expType);
9134                      exp.expType = exp.op.exp1.destType;
9135                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
9136                   }
9137                }
9138             }
9139             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
9140             {
9141                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
9142                {
9143                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
9144                   // Convert e.g. / 4 into / 4.0
9145                   exp.op.exp1.destType = type2._class.registered.dataType;
9146                   if(type2._class.registered.dataType)
9147                      type2._class.registered.dataType.refCount++;
9148                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false, false);
9149                }
9150                if(exp.op.op == '!')
9151                {
9152                   exp.expType = MkClassType("bool");
9153                   exp.expType.truth = true;
9154                }
9155                else
9156                {
9157                   exp.expType = type2;
9158                   if(type2) type2.refCount++;
9159                }
9160             }
9161             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
9162             {
9163                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
9164                {
9165                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
9166                   // Convert e.g. / 4 into / 4.0
9167                   exp.op.exp2.destType = type1._class.registered.dataType;
9168                   if(type1._class.registered.dataType)
9169                      type1._class.registered.dataType.refCount++;
9170                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false, false);
9171                }
9172                exp.expType = type1;
9173                if(type1) type1.refCount++;
9174             }
9175          }
9176
9177          yylloc = exp.loc;
9178          if(exp.op.exp1 && !exp.op.exp1.expType)
9179          {
9180             char expString[10000];
9181             expString[0] = '\0';
9182             if(inCompiler)
9183             {
9184                PrintExpression(exp.op.exp1, expString);
9185                ChangeCh(expString, '\n', ' ');
9186             }
9187             if(expString[0])
9188                Compiler_Error($"couldn't determine type of %s\n", expString);
9189          }
9190          if(exp.op.exp2 && !exp.op.exp2.expType)
9191          {
9192             char expString[10240];
9193             expString[0] = '\0';
9194             if(inCompiler)
9195             {
9196                PrintExpression(exp.op.exp2, expString);
9197                ChangeCh(expString, '\n', ' ');
9198             }
9199             if(expString[0])
9200                Compiler_Error($"couldn't determine type of %s\n", expString);
9201          }
9202
9203          if(boolResult)
9204          {
9205             FreeType(exp.expType);
9206             exp.expType = MkClassType("bool");
9207             exp.expType.truth = true;
9208          }
9209
9210          if(exp.op.op != SIZEOF)
9211             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
9212                (!exp.op.exp2 || exp.op.exp2.isConstant);
9213
9214          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
9215          {
9216             DeclareType(curExternal, exp.op.exp2.expType, true, false);
9217          }
9218
9219          if(exp.op.op == DELETE && exp.op.exp2 && exp.op.exp2.expType && exp.op.exp2.expType.specConst)
9220             Compiler_Warning($"deleting const qualified object\n");
9221
9222          yylloc = oldyylloc;
9223
9224          FreeType(dummy);
9225          if(type2)
9226             FreeType(type2);
9227          break;
9228       }
9229       case bracketsExp:
9230       case extensionExpressionExp:
9231       {
9232          Expression e;
9233          exp.isConstant = true;
9234          for(e = exp.list->first; e; e = e.next)
9235          {
9236             //bool inced = false;
9237             if(!e.next)
9238             {
9239                FreeType(e.destType);
9240                e.opDestType = exp.opDestType;
9241                e.destType = exp.destType;
9242                if(e.destType) { exp.destType.refCount++; /*e.destType.count++; inced = true;*/ }
9243             }
9244             ProcessExpressionType(e);
9245             /*if(inced)
9246                exp.destType.count--;*/
9247             if(!exp.expType && !e.next)
9248             {
9249                exp.expType = e.expType;
9250                if(e.expType) e.expType.refCount++;
9251             }
9252             if(!e.isConstant)
9253                exp.isConstant = false;
9254          }
9255
9256          // In case a cast became a member...
9257          e = exp.list->first;
9258          if(!e.next && e.type == memberExp)
9259          {
9260             // Preserve prev, next
9261             Expression next = exp.next, prev = exp.prev;
9262
9263
9264             FreeType(exp.expType);
9265             FreeType(exp.destType);
9266             delete exp.list;
9267
9268             *exp = *e;
9269
9270             exp.prev = prev;
9271             exp.next = next;
9272
9273             delete e;
9274
9275             ProcessExpressionType(exp);
9276          }
9277          break;
9278       }
9279       case indexExp:
9280       {
9281          Expression e;
9282          exp.isConstant = true;
9283
9284          ProcessExpressionType(exp.index.exp);
9285          if(!exp.index.exp.isConstant)
9286             exp.isConstant = false;
9287
9288          if(exp.index.exp.expType)
9289          {
9290             Type source = exp.index.exp.expType;
9291             if(source.kind == classType && source._class && source._class.registered)
9292             {
9293                Class _class = source._class.registered;
9294                Class c = _class.templateClass ? _class.templateClass : _class;
9295                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
9296                {
9297                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
9298
9299                   if(exp.index.index && exp.index.index->last)
9300                   {
9301                      Type type = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
9302
9303                      if(type.kind == classType) type.constant = true;
9304                      else if(type.kind == pointerType)
9305                      {
9306                         Type t = type;
9307                         while(t.kind == pointerType) t = t.type;
9308                         t.constant = true;
9309                      }
9310
9311                      ((Expression)exp.index.index->last).destType = type;
9312                   }
9313                }
9314             }
9315          }
9316
9317          for(e = exp.index.index->first; e; e = e.next)
9318          {
9319             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
9320             {
9321                if(e.destType) FreeType(e.destType);
9322                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
9323             }
9324             ProcessExpressionType(e);
9325             if(!e.next)
9326             {
9327                // Check if this type is int
9328             }
9329             if(!e.isConstant)
9330                exp.isConstant = false;
9331          }
9332
9333          if(!exp.expType)
9334             exp.expType = Dereference(exp.index.exp.expType);
9335          if(exp.expType)
9336             DeclareType(curExternal, exp.expType, true, false);
9337          break;
9338       }
9339       case callExp:
9340       {
9341          Expression e;
9342          Type functionType;
9343          Type methodType = null;
9344          char name[1024];
9345          name[0] = '\0';
9346
9347          if(inCompiler)
9348          {
9349             PrintExpression(exp.call.exp,  name);
9350             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
9351             {
9352                //exp.call.exp.expType = null;
9353                PrintExpression(exp.call.exp,  name);
9354             }
9355          }
9356          if(exp.call.exp.type == identifierExp)
9357          {
9358             Expression idExp = exp.call.exp;
9359             Identifier id = idExp.identifier;
9360             if(!strcmp(id.string, "__builtin_frame_address"))
9361             {
9362                exp.expType = ProcessTypeString("void *", true);
9363                if(exp.call.arguments && exp.call.arguments->first)
9364                   ProcessExpressionType(exp.call.arguments->first);
9365                break;
9366             }
9367             else if(!strcmp(id.string, "__ENDIAN_PAD"))
9368             {
9369                exp.expType = ProcessTypeString("int", true);
9370                if(exp.call.arguments && exp.call.arguments->first)
9371                   ProcessExpressionType(exp.call.arguments->first);
9372                break;
9373             }
9374             else if(!strcmp(id.string, "Max") ||
9375                !strcmp(id.string, "Min") ||
9376                !strcmp(id.string, "Sgn") ||
9377                !strcmp(id.string, "Abs"))
9378             {
9379                Expression a = null;
9380                Expression b = null;
9381                Expression tempExp1 = null, tempExp2 = null;
9382                if((!strcmp(id.string, "Max") ||
9383                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
9384                {
9385                   a = exp.call.arguments->first;
9386                   b = exp.call.arguments->last;
9387                   tempExp1 = a;
9388                   tempExp2 = b;
9389                }
9390                else if(exp.call.arguments->count == 1)
9391                {
9392                   a = exp.call.arguments->first;
9393                   tempExp1 = a;
9394                }
9395
9396                if(a)
9397                {
9398                   exp.call.arguments->Clear();
9399                   idExp.identifier = null;
9400
9401                   FreeExpContents(exp);
9402
9403                   ProcessExpressionType(a);
9404                   if(b)
9405                      ProcessExpressionType(b);
9406
9407                   exp.type = bracketsExp;
9408                   exp.list = MkList();
9409
9410                   if(a.expType && (!b || b.expType))
9411                   {
9412                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9413                      {
9414                         // Use the simpleStruct name/ids for now...
9415                         if(inCompiler)
9416                         {
9417                            OldList * specs = MkList();
9418                            OldList * decls = MkList();
9419                            Declaration decl;
9420                            char temp1[1024], temp2[1024];
9421
9422                            GetTypeSpecs(a.expType, specs);
9423
9424                            if(a && !a.isConstant && a.type != identifierExp)
9425                            {
9426                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9427                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9428                               tempExp1 = QMkExpId(temp1);
9429                               tempExp1.expType = a.expType;
9430                               if(a.expType)
9431                                  a.expType.refCount++;
9432                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9433                            }
9434                            if(b && !b.isConstant && b.type != identifierExp)
9435                            {
9436                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9437                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9438                               tempExp2 = QMkExpId(temp2);
9439                               tempExp2.expType = b.expType;
9440                               if(b.expType)
9441                                  b.expType.refCount++;
9442                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9443                            }
9444
9445                            decl = MkDeclaration(specs, decls);
9446                            if(!curCompound.compound.declarations)
9447                               curCompound.compound.declarations = MkList();
9448                            curCompound.compound.declarations->Insert(null, decl);
9449                         }
9450                      }
9451                   }
9452
9453                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9454                   {
9455                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9456                      ListAdd(exp.list,
9457                         MkExpCondition(MkExpBrackets(MkListOne(
9458                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9459                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9460                      exp.expType = a.expType;
9461                      if(a.expType)
9462                         a.expType.refCount++;
9463                   }
9464                   else if(!strcmp(id.string, "Abs"))
9465                   {
9466                      ListAdd(exp.list,
9467                         MkExpCondition(MkExpBrackets(MkListOne(
9468                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9469                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9470                      exp.expType = a.expType;
9471                      if(a.expType)
9472                         a.expType.refCount++;
9473                   }
9474                   else if(!strcmp(id.string, "Sgn"))
9475                   {
9476                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9477                      ListAdd(exp.list,
9478                         MkExpCondition(MkExpBrackets(MkListOne(
9479                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9480                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9481                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9482                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9483                      exp.expType = ProcessTypeString("int", false);
9484                   }
9485
9486                   FreeExpression(tempExp1);
9487                   if(tempExp2) FreeExpression(tempExp2);
9488
9489                   FreeIdentifier(id);
9490                   break;
9491                }
9492             }
9493          }
9494
9495          {
9496             Type dummy
9497             {
9498                count = 1;
9499                refCount = 1;
9500             };
9501             if(!exp.call.exp.destType)
9502             {
9503                exp.call.exp.destType = dummy;
9504                dummy.refCount++;
9505             }
9506             ProcessExpressionType(exp.call.exp);
9507             if(exp.call.exp.destType == dummy)
9508             {
9509                FreeType(dummy);
9510                exp.call.exp.destType = null;
9511             }
9512             FreeType(dummy);
9513          }
9514
9515          // Check argument types against parameter types
9516          functionType = exp.call.exp.expType;
9517
9518          if(functionType && functionType.kind == TypeKind::methodType)
9519          {
9520             methodType = functionType;
9521             functionType = methodType.method.dataType;
9522
9523             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9524             // TOCHECK: Instead of doing this here could this be done per param?
9525             if(exp.call.exp.expType.usedClass)
9526             {
9527                char typeString[1024];
9528                typeString[0] = '\0';
9529                {
9530                   Symbol back = functionType.thisClass;
9531                   // Do not output class specifier here (thisclass was added to this)
9532                   functionType.thisClass = null;
9533                   PrintType(functionType, typeString, true, true);
9534                   functionType.thisClass = back;
9535                }
9536                if(strstr(typeString, "thisclass"))
9537                {
9538                   OldList * specs = MkList();
9539                   Declarator decl;
9540                   {
9541                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9542
9543                      decl = SpecDeclFromString(typeString, specs, null);
9544
9545                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9546                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9547                         exp.call.exp.expType.usedClass))
9548                         thisClassParams = false;
9549
9550                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9551                      {
9552                         Class backupThisClass = thisClass;
9553                         thisClass = exp.call.exp.expType.usedClass;
9554                         ProcessDeclarator(decl, true);
9555                         thisClass = backupThisClass;
9556                      }
9557
9558                      thisClassParams = true;
9559
9560                      functionType = ProcessType(specs, decl);
9561                      functionType.refCount = 0;
9562                      FinishTemplatesContext(context);
9563
9564                      // Mark parameters that were 'thisclass'
9565                      {
9566                         Type p, op;
9567                         for(p = functionType.params.first, op = methodType.method.dataType.params.first; p && op; p = p.next, op = op.next)
9568                         {
9569                            //p.wasThisClass = op.kind == thisClassType;
9570                            if(op.kind == thisClassType)
9571                               p.thisClassFrom = methodType.method._class;
9572                         }
9573                      }
9574                      if(methodType.method.dataType.returnType.kind == thisClassType)
9575                      {
9576                         // functionType.returnType.wasThisClass = true;
9577                         functionType.returnType.thisClassFrom = methodType.method._class;
9578                      }
9579                   }
9580
9581                   FreeList(specs, FreeSpecifier);
9582                   FreeDeclarator(decl);
9583                 }
9584             }
9585          }
9586          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9587          {
9588             Type type = functionType.type;
9589             if(!functionType.refCount)
9590             {
9591                functionType.type = null;
9592                FreeType(functionType);
9593             }
9594             //methodType = functionType;
9595             functionType = type;
9596          }
9597          if(functionType && functionType.kind != TypeKind::functionType)
9598          {
9599             Compiler_Error($"called object %s is not a function\n", name);
9600          }
9601          else if(functionType)
9602          {
9603             bool emptyParams = false, noParams = false;
9604             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9605             Type type = functionType.params.first;
9606             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9607             int extra = 0;
9608             Location oldyylloc = yylloc;
9609
9610             if(!type) emptyParams = true;
9611
9612             // WORKING ON THIS:
9613             if(functionType.extraParam && e && functionType.thisClass)
9614             {
9615                e.destType = MkClassType(functionType.thisClass.string);
9616                e = e.next;
9617             }
9618
9619             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9620             // Fixed #141 by adding '&& !functionType.extraParam'
9621             if(!functionType.staticMethod && !functionType.extraParam)
9622             {
9623                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9624                   memberExp.member.exp.expType._class)
9625                {
9626                   type = MkClassType(memberExp.member.exp.expType._class.string);
9627                   if(e)
9628                   {
9629                      e.destType = type;
9630                      e = e.next;
9631                      type = functionType.params.first;
9632                   }
9633                   else
9634                      type.refCount = 0;
9635                }
9636                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9637                {
9638                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9639                   type.byReference = functionType.byReference;
9640                   type.typedByReference = functionType.typedByReference;
9641                   if(e)
9642                   {
9643                      // Allow manually passing a class for typed object
9644                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9645                         e = e.next;
9646                      e.destType = type;
9647                      e = e.next;
9648                      type = functionType.params.first;
9649                   }
9650                   else
9651                      type.refCount = 0;
9652                   //extra = 1;
9653                }
9654             }
9655
9656             if(type && type.kind == voidType)
9657             {
9658                noParams = true;
9659                if(!type.refCount) FreeType(type);
9660                type = null;
9661             }
9662
9663             for( ; e; e = e.next)
9664             {
9665                if(!type && !emptyParams)
9666                {
9667                   yylloc = e.loc;
9668                   if(methodType && methodType.methodClass)
9669                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9670                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9671                         noParams ? 0 : functionType.params.count);
9672                   else
9673                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9674                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9675                         noParams ? 0 : functionType.params.count);
9676                   break;
9677                }
9678
9679                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9680                {
9681                   Type templatedType = null;
9682                   Class _class = methodType.usedClass;
9683                   ClassTemplateParameter curParam = null;
9684                   int id = 0;
9685                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9686                   {
9687                      Class sClass;
9688                      for(sClass = _class; sClass; sClass = sClass.base)
9689                      {
9690                         if(sClass.templateClass) sClass = sClass.templateClass;
9691                         id = 0;
9692                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9693                         {
9694                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9695                            {
9696                               Class nextClass;
9697                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9698                               {
9699                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9700                                  id += nextClass.templateParams.count;
9701                               }
9702                               break;
9703                            }
9704                            id++;
9705                         }
9706                         if(curParam) break;
9707                      }
9708                   }
9709                   if(curParam && _class.templateArgs[id].dataTypeString)
9710                   {
9711                      bool constant = type.constant;
9712                      ClassTemplateArgument arg = _class.templateArgs[id];
9713                      {
9714                         Context context = SetupTemplatesContext(_class);
9715
9716                         /*if(!arg.dataType)
9717                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9718                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9719                         FinishTemplatesContext(context);
9720                      }
9721
9722                      if(templatedType.kind == classType && constant) templatedType.constant = true;
9723                      else if(templatedType.kind == pointerType)
9724                      {
9725                         Type t = templatedType.type;
9726                         while(t.kind == pointerType) t = t.type;
9727                         if(constant) t.constant = constant;
9728                      }
9729
9730                      e.destType = templatedType;
9731                      if(templatedType)
9732                      {
9733                         templatedType.passAsTemplate = true;
9734                         // templatedType.refCount++;
9735                      }
9736                   }
9737                   else
9738                   {
9739                      e.destType = type;
9740                      if(type) type.refCount++;
9741                   }
9742                }
9743                else
9744                {
9745                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9746                   {
9747                      e.destType = type.prev;
9748                      e.destType.refCount++;
9749                   }
9750                   else
9751                   {
9752                      e.destType = type;
9753                      if(type) type.refCount++;
9754                   }
9755                }
9756                // Don't reach the end for the ellipsis
9757                if(type && type.kind != ellipsisType)
9758                {
9759                   Type next = type.next;
9760                   if(!type.refCount) FreeType(type);
9761                   type = next;
9762                }
9763             }
9764
9765             if(type && type.kind != ellipsisType)
9766             {
9767                if(methodType && methodType.methodClass)
9768                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9769                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9770                      functionType.params.count + extra);
9771                else
9772                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9773                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9774                      functionType.params.count + extra);
9775             }
9776             yylloc = oldyylloc;
9777             if(type && !type.refCount) FreeType(type);
9778          }
9779          else
9780          {
9781             functionType = Type
9782             {
9783                refCount = 0;
9784                kind = TypeKind::functionType;
9785             };
9786
9787             if(exp.call.exp.type == identifierExp)
9788             {
9789                char * string = exp.call.exp.identifier.string;
9790                if(inCompiler)
9791                {
9792                   Symbol symbol;
9793                   Location oldyylloc = yylloc;
9794
9795                   yylloc = exp.call.exp.identifier.loc;
9796                   if(strstr(string, "__builtin_") == string)
9797                   {
9798                      if(exp.destType)
9799                      {
9800                         functionType.returnType = exp.destType;
9801                         exp.destType.refCount++;
9802                      }
9803                   }
9804                   else
9805                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9806                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9807                   globalContext.symbols.Add((BTNode)symbol);
9808                   if(strstr(symbol.string, "::"))
9809                      globalContext.hasNameSpace = true;
9810
9811                   yylloc = oldyylloc;
9812                }
9813             }
9814             else if(exp.call.exp.type == memberExp)
9815             {
9816                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9817                   exp.call.exp.member.member.string);*/
9818             }
9819             else
9820                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9821
9822             if(!functionType.returnType)
9823             {
9824                functionType.returnType = Type
9825                {
9826                   refCount = 1;
9827                   kind = intType;
9828                };
9829             }
9830          }
9831          if(functionType && functionType.kind == TypeKind::functionType)
9832          {
9833             exp.expType = functionType.returnType;
9834
9835             if(functionType.returnType)
9836                functionType.returnType.refCount++;
9837
9838             if(!functionType.refCount)
9839                FreeType(functionType);
9840          }
9841
9842          if(exp.call.arguments)
9843          {
9844             for(e = exp.call.arguments->first; e; e = e.next)
9845                ProcessExpressionType(e);
9846          }
9847          break;
9848       }
9849       case memberExp:
9850       {
9851          Type type;
9852          Location oldyylloc = yylloc;
9853          bool thisPtr;
9854          Expression checkExp = exp.member.exp;
9855          while(checkExp)
9856          {
9857             if(checkExp.type == castExp)
9858                checkExp = checkExp.cast.exp;
9859             else if(checkExp.type == bracketsExp)
9860                checkExp = checkExp.list ? checkExp.list->first : null;
9861             else
9862                break;
9863          }
9864
9865          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9866          exp.thisPtr = thisPtr;
9867
9868          // DOING THIS LATER NOW...
9869          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9870          {
9871             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9872             /* TODO: Name Space Fix ups
9873             if(!exp.member.member.classSym)
9874                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9875             */
9876          }
9877
9878          ProcessExpressionType(exp.member.exp);
9879          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9880             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9881          {
9882             exp.isConstant = false;
9883          }
9884          else
9885             exp.isConstant = exp.member.exp.isConstant;
9886          type = exp.member.exp.expType;
9887
9888          yylloc = exp.loc;
9889
9890          if(type && (type.kind == templateType))
9891          {
9892             Class _class = thisClass ? thisClass : currentClass;
9893             ClassTemplateParameter param = null;
9894             if(_class)
9895             {
9896                for(param = _class.templateParams.first; param; param = param.next)
9897                {
9898                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9899                      break;
9900                }
9901             }
9902             if(param && param.defaultArg.member)
9903             {
9904                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9905                if(argExp)
9906                {
9907                   Expression expMember = exp.member.exp;
9908                   Declarator decl;
9909                   OldList * specs = MkList();
9910                   char thisClassTypeString[1024];
9911
9912                   FreeIdentifier(exp.member.member);
9913
9914                   ProcessExpressionType(argExp);
9915
9916                   {
9917                      char * colon = strstr(param.defaultArg.memberString, "::");
9918                      if(colon)
9919                      {
9920                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9921                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9922                      }
9923                      else
9924                         strcpy(thisClassTypeString, _class.fullName);
9925                   }
9926
9927                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9928
9929                   exp.expType = ProcessType(specs, decl);
9930                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9931                   {
9932                      Class expClass = exp.expType._class.registered;
9933                      Class cClass = null;
9934                      int paramCount = 0;
9935                      int lastParam = -1;
9936
9937                      char templateString[1024];
9938                      ClassTemplateParameter param;
9939                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9940                      for(cClass = expClass; cClass; cClass = cClass.base)
9941                      {
9942                         int p = 0;
9943                         for(param = cClass.templateParams.first; param; param = param.next)
9944                         {
9945                            int id = p;
9946                            Class sClass;
9947                            ClassTemplateArgument arg;
9948                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9949                            arg = expClass.templateArgs[id];
9950
9951                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9952                            {
9953                               ClassTemplateParameter cParam;
9954                               //int p = numParams - sClass.templateParams.count;
9955                               int p = 0;
9956                               Class nextClass;
9957                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9958
9959                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9960                               {
9961                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9962                                  {
9963                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9964                                     {
9965                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9966                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9967                                        break;
9968                                     }
9969                                  }
9970                               }
9971                            }
9972
9973                            {
9974                               char argument[256];
9975                               argument[0] = '\0';
9976                               /*if(arg.name)
9977                               {
9978                                  strcat(argument, arg.name.string);
9979                                  strcat(argument, " = ");
9980                               }*/
9981                               switch(param.type)
9982                               {
9983                                  case expression:
9984                                  {
9985                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9986                                     char expString[1024];
9987                                     OldList * specs = MkList();
9988                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9989                                     Expression exp;
9990                                     char * string = PrintHexUInt64(arg.expression.ui64);
9991                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9992                                     delete string;
9993
9994                                     ProcessExpressionType(exp);
9995                                     ComputeExpression(exp);
9996                                     expString[0] = '\0';
9997                                     PrintExpression(exp, expString);
9998                                     strcat(argument, expString);
9999                                     // delete exp;
10000                                     FreeExpression(exp);
10001                                     break;
10002                                  }
10003                                  case identifier:
10004                                  {
10005                                     strcat(argument, arg.member.name);
10006                                     break;
10007                                  }
10008                                  case TemplateParameterType::type:
10009                                  {
10010                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10011                                     {
10012                                        if(!strcmp(arg.dataTypeString, "thisclass"))
10013                                           strcat(argument, thisClassTypeString);
10014                                        else
10015                                           strcat(argument, arg.dataTypeString);
10016                                     }
10017                                     break;
10018                                  }
10019                               }
10020                               if(argument[0])
10021                               {
10022                                  if(paramCount) strcat(templateString, ", ");
10023                                  if(lastParam != p - 1)
10024                                  {
10025                                     strcat(templateString, param.name);
10026                                     strcat(templateString, " = ");
10027                                  }
10028                                  strcat(templateString, argument);
10029                                  paramCount++;
10030                                  lastParam = p;
10031                               }
10032                               p++;
10033                            }
10034                         }
10035                      }
10036                      {
10037                         int len = strlen(templateString);
10038                         if(templateString[len-1] == '>') templateString[len++] = ' ';
10039                         templateString[len++] = '>';
10040                         templateString[len++] = '\0';
10041                      }
10042                      {
10043                         Context context = SetupTemplatesContext(_class);
10044                         FreeType(exp.expType);
10045                         exp.expType = ProcessTypeString(templateString, false);
10046                         FinishTemplatesContext(context);
10047                      }
10048                   }
10049
10050                   if(!expMember.expType.isPointerType)
10051                      expMember = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uintptr")), null), expMember);
10052                   // *([expType] *)(((byte *)(uintptr)[exp.member.exp]) + [argExp].member.offset)
10053                   exp.type = bracketsExp;
10054                   exp.list = MkListOne(MkExpOp(null, '*',
10055                   /*opExp;
10056                   exp.op.op = '*';
10057                   exp.op.exp1 = null;
10058                   exp.op.exp2 = */
10059                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
10060                      MkExpBrackets(MkListOne(
10061                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
10062                            expMember))),
10063                               '+',
10064                               MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
10065                               '+',
10066                               MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
10067
10068                            ));
10069                }
10070             }
10071             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
10072                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
10073             {
10074                type = ProcessTemplateParameterType(type.templateParameter);
10075             }
10076          }
10077          // TODO: *** This seems to be where we should add method support for all basic types ***
10078          if(type && (type.kind == templateType));
10079          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
10080                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
10081                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
10082                           (type.kind == pointerType && type.type.kind == charType)))
10083          {
10084             Identifier id = exp.member.member;
10085             TypeKind typeKind = type.kind;
10086             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10087             if(typeKind == subClassType && exp.member.exp.type == classExp)
10088             {
10089                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
10090                typeKind = classType;
10091             }
10092
10093             if(id)
10094             {
10095                if(typeKind == intType || typeKind == enumType)
10096                   _class = eSystem_FindClass(privateModule, "int");
10097                else if(!_class)
10098                {
10099                   if(type.kind == classType && type._class && type._class.registered)
10100                   {
10101                      _class = type._class.registered;
10102                   }
10103                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
10104                   {
10105                      _class = FindClass("char *").registered;
10106                   }
10107                   else if(type.kind == pointerType)
10108                   {
10109                      _class = eSystem_FindClass(privateModule, "uintptr");
10110                      FreeType(exp.expType);
10111                      exp.expType = ProcessTypeString("uintptr", false);
10112                      exp.byReference = true;
10113                   }
10114                   else
10115                   {
10116                      char string[1024] = "";
10117                      Symbol classSym;
10118                      PrintTypeNoConst(type, string, false, true);
10119                      classSym = FindClass(string);
10120                      if(classSym) _class = classSym.registered;
10121                   }
10122                }
10123             }
10124
10125             if(_class && id)
10126             {
10127                /*bool thisPtr =
10128                   (exp.member.exp.type == identifierExp &&
10129                   !strcmp(exp.member.exp.identifier.string, "this"));*/
10130                Property prop = null;
10131                Method method = null;
10132                DataMember member = null;
10133                Property revConvert = null;
10134                ClassProperty classProp = null;
10135
10136                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
10137                   exp.member.memberType = propertyMember;
10138
10139                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
10140                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
10141
10142                if(typeKind != subClassType)
10143                {
10144                   // Prioritize data members over properties for "this"
10145                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
10146                   {
10147                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10148                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
10149                      {
10150                         prop = eClass_FindProperty(_class, id.string, privateModule);
10151                         if(prop)
10152                            member = null;
10153                      }
10154                      if(!member && !prop)
10155                         prop = eClass_FindProperty(_class, id.string, privateModule);
10156                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
10157                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
10158                         exp.member.thisPtr = true;
10159                   }
10160                   // Prioritize properties over data members otherwise
10161                   else
10162                   {
10163                      bool useMemberForNonConst = false;
10164                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
10165                      if(!id.classSym)
10166                      {
10167                         prop = eClass_FindProperty(_class, id.string, null);
10168
10169                         useMemberForNonConst = prop && exp.destType &&
10170                            ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10171                               !strncmp(prop.dataTypeString, "const ", 6);
10172
10173                         if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10174                            member = eClass_FindDataMember(_class, id.string, null, null, null);
10175                      }
10176
10177                      if((!prop || useMemberForNonConst) && !member)
10178                      {
10179                         method = useMemberForNonConst ? null : eClass_FindMethod(_class, id.string, null);
10180                         if(!method)
10181                         {
10182                            prop = eClass_FindProperty(_class, id.string, privateModule);
10183
10184                            useMemberForNonConst |= prop && exp.destType &&
10185                               ( (exp.destType.kind == classType && !exp.destType.constant) || ((exp.destType.kind == pointerType || exp.destType.kind == arrayType) && exp.destType.type && !exp.destType.type.constant) ) &&
10186                                  !strncmp(prop.dataTypeString, "const ", 6);
10187
10188                            if(useMemberForNonConst || !id._class || !id._class.name || strcmp(id._class.name, "property"))
10189                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
10190                         }
10191                      }
10192
10193                      if(member && prop)
10194                      {
10195                         if(useMemberForNonConst || (member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class)))
10196                            prop = null;
10197                         else
10198                            member = null;
10199                      }
10200                   }
10201                }
10202                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
10203                   method = eClass_FindMethod(_class, id.string, privateModule);
10204                if(!prop && !member && !method)
10205                {
10206                   if(typeKind == subClassType)
10207                   {
10208                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
10209                      if(classProp)
10210                      {
10211                         exp.member.memberType = classPropertyMember;
10212                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
10213                      }
10214                      else
10215                      {
10216                         // Assume this is a class_data member
10217                         char structName[1024];
10218                         Identifier id = exp.member.member;
10219                         Expression classExp = exp.member.exp;
10220                         type.refCount++;
10221
10222                         FreeType(classExp.expType);
10223                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
10224
10225                         strcpy(structName, "__ecereClassData_");
10226                         FullClassNameCat(structName, type._class.string, false);
10227                         exp.type = pointerExp;
10228                         exp.member.member = id;
10229
10230                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10231                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10232                               MkExpBrackets(MkListOne(MkExpOp(
10233                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10234                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
10235                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
10236                                  )));
10237
10238                         FreeType(type);
10239
10240                         ProcessExpressionType(exp);
10241                         return;
10242                      }
10243                   }
10244                   else
10245                   {
10246                      // Check for reverse conversion
10247                      // (Convert in an instantiation later, so that we can use
10248                      //  deep properties system)
10249                      Symbol classSym = FindClass(id.string);
10250                      if(classSym)
10251                      {
10252                         Class convertClass = classSym.registered;
10253                         if(convertClass)
10254                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
10255                      }
10256                   }
10257                }
10258
10259                //if(!exp.member.exp.destType)
10260                if(exp.member.exp.destType)
10261                   FreeType(exp.member.exp.destType);
10262                {
10263                   if(method && !method._class.symbol)
10264                      method._class.symbol = FindClass(method._class.fullName);
10265                   if(prop && !prop._class.symbol)
10266                      prop._class.symbol = FindClass(prop._class.fullName);
10267
10268                   exp.member.exp.destType = Type
10269                   {
10270                      refCount = 1;
10271                      kind = classType;
10272                      _class = prop ? prop._class.symbol : method ? method._class.symbol : _class.symbol;
10273                      // wasThisClass = type ? type.wasThisClass : false;
10274                      thisClassFrom = type ? type.thisClassFrom : null;
10275                   };
10276                }
10277
10278                if(prop)
10279                {
10280                   exp.member.memberType = propertyMember;
10281                   if(!prop.dataType)
10282                      ProcessPropertyType(prop);
10283                   exp.expType = prop.dataType;
10284                   if(!strcmp(_class.base.fullName, "eda::Row") && !exp.expType.constant && !exp.destType)
10285                   {
10286                      Type type { };
10287                      CopyTypeInto(type, exp.expType);
10288                      type.refCount = 1;
10289                      type.constant = true;
10290                      exp.expType = type;
10291                   }
10292                   else if(prop.dataType)
10293                      prop.dataType.refCount++;
10294                }
10295                else if(member)
10296                {
10297                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10298                   {
10299                      FreeExpContents(exp);
10300                      exp.type = identifierExp;
10301                      exp.identifier = MkIdentifier("class");
10302                      ProcessExpressionType(exp);
10303                      return;
10304                   }
10305
10306                   exp.member.memberType = dataMember;
10307                   DeclareStruct(curExternal, _class.fullName, false, true);
10308                   if(member._class != _class)
10309                      DeclareStruct(curExternal, member._class.fullName, false, true);
10310
10311                   if(!member.dataType)
10312                   {
10313                      Context context = SetupTemplatesContext(_class);
10314                      member.dataType = ProcessTypeString(member.dataTypeString, false);
10315                      FinishTemplatesContext(context);
10316                   }
10317                   if(exp.member.exp.expType.kind == classType && exp.member.exp.expType._class && exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == bitClass)
10318                      member.dataType.bitMemberSize = ((BitMember)member).size;
10319                   exp.expType = member.dataType;
10320                   if(member.dataType) member.dataType.refCount++;
10321                }
10322                else if(revConvert)
10323                {
10324                   exp.member.memberType = reverseConversionMember;
10325                   exp.expType = MkClassType(revConvert._class.fullName);
10326                }
10327                else if(method)
10328                {
10329                   //if(inCompiler)
10330                   {
10331                      /*if(id._class)
10332                      {
10333                         exp.type = identifierExp;
10334                         exp.identifier = exp.member.member;
10335                      }
10336                      else*/
10337                         exp.member.memberType = methodMember;
10338                   }
10339                   if(!method.dataType)
10340                      ProcessMethodType(method);
10341                   exp.expType = Type
10342                   {
10343                      refCount = 1;
10344                      kind = methodType;
10345                      method = method;
10346                   };
10347
10348                   // Tricky spot here... To use instance versus class virtual table
10349                   // Put it back to what it was... What did we break?
10350
10351                   // Had to put it back for overriding Main of Thread global instance
10352
10353                   //exp.expType.methodClass = _class;
10354                   exp.expType.methodClass = (id && id._class) ? _class : null;
10355
10356                   // Need the actual class used for templated classes
10357                   exp.expType.usedClass = _class;
10358                }
10359                else if(!classProp)
10360                {
10361                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
10362                   {
10363                      FreeExpContents(exp);
10364                      exp.type = identifierExp;
10365                      exp.identifier = MkIdentifier("class");
10366                      FreeType(exp.expType);
10367                      exp.expType = MkClassType("ecere::com::Class");
10368                      return;
10369                   }
10370                   yylloc = exp.member.member.loc;
10371                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
10372                   if(inCompiler)
10373                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
10374                }
10375
10376                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
10377                {
10378                   Class tClass;
10379
10380                   tClass = type._class && type._class.registered ? type._class.registered : _class;
10381                   while(tClass && !tClass.templateClass) tClass = tClass.base;
10382
10383                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
10384                   {
10385                      int id = 0;
10386                      ClassTemplateParameter curParam = null;
10387                      Class sClass;
10388
10389                      for(sClass = tClass; sClass; sClass = sClass.base)
10390                      {
10391                         id = 0;
10392                         if(sClass.templateClass) sClass = sClass.templateClass;
10393                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10394                         {
10395                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
10396                            {
10397                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10398                                  id += sClass.templateParams.count;
10399                               break;
10400                            }
10401                            id++;
10402                         }
10403                         if(curParam) break;
10404                      }
10405
10406                      if(curParam && tClass.templateArgs[id].dataTypeString)
10407                      {
10408                         ClassTemplateArgument arg = tClass.templateArgs[id];
10409                         Context context = SetupTemplatesContext(tClass);
10410                         bool constant = exp.expType.constant;
10411                         bool passAsTemplate = false;
10412                         Class thisClassFrom = null;
10413                         Type t = ProcessTypeString(exp.expType.templateParameter.dataTypeString, false);
10414                         if(t && t.kind == classType && t._class)
10415                            thisClassFrom = t._class.registered;
10416                         else
10417                            // Mark that 'thisClassFrom' was set to something
10418                            thisClassFrom = eSystem_FindClass(GetPrivateModule(), "class");
10419
10420                         FreeType(t);
10421
10422                         passAsTemplate = tClass.templateClass && (exp.expType.kind != templateType ||
10423                            (!exp.expType.templateParameter || (!exp.expType.templateParameter.dataTypeString && !exp.expType.templateParameter.dataType)));
10424
10425                         /*if(!arg.dataType)
10426                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10427                         FreeType(exp.expType);
10428
10429                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
10430                         exp.expType.thisClassFrom = thisClassFrom;
10431                         if(exp.expType.kind == classType && constant) exp.expType.constant = true;
10432                         else if(exp.expType.kind == pointerType)
10433                         {
10434                            Type t = exp.expType.type;
10435                            while(t.kind == pointerType) t = t.type;
10436                            if(constant) t.constant = constant;
10437                         }
10438                         if(exp.expType)
10439                         {
10440                            if(exp.expType.kind == thisClassType)
10441                            {
10442                               FreeType(exp.expType);
10443                               exp.expType = ReplaceThisClassType(_class);
10444                            }
10445
10446                            if(passAsTemplate)
10447                               exp.expType.passAsTemplate = true;
10448                            //exp.expType.refCount++;
10449                            if(!exp.destType)
10450                            {
10451                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
10452                               if(exp.destType.kind == classType && constant) exp.destType.constant = true;
10453                               else if(exp.destType.kind == pointerType)
10454                               {
10455                                  Type t = exp.destType.type;
10456                                  while(t.kind == pointerType) t = t.type;
10457                                  if(constant) t.constant = constant;
10458                               }
10459
10460                               //exp.destType.refCount++;
10461
10462                               if(exp.destType.kind == thisClassType)
10463                               {
10464                                  FreeType(exp.destType);
10465                                  exp.destType = ReplaceThisClassType(_class);
10466                               }
10467                            }
10468                         }
10469                         FinishTemplatesContext(context);
10470                      }
10471                   }
10472                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
10473                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
10474                   {
10475                      int id = 0;
10476                      ClassTemplateParameter curParam = null;
10477                      Class sClass;
10478
10479                      for(sClass = tClass; sClass; sClass = sClass.base)
10480                      {
10481                         id = 0;
10482                         if(sClass.templateClass) sClass = sClass.templateClass;
10483                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
10484                         {
10485                            if(curParam.type == TemplateParameterType::type &&
10486                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
10487                            {
10488                               for(sClass = sClass.base; sClass; sClass = sClass.base)
10489                                  id += sClass.templateParams.count;
10490                               break;
10491                            }
10492                            id++;
10493                         }
10494                         if(curParam) break;
10495                      }
10496
10497                      if(curParam)
10498                      {
10499                         ClassTemplateArgument arg = tClass.templateArgs[id];
10500                         Context context = SetupTemplatesContext(tClass);
10501                         Type basicType;
10502                         /*if(!arg.dataType)
10503                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10504
10505                         basicType = ProcessTypeString(arg.dataTypeString, false);
10506                         if(basicType)
10507                         {
10508                            if(basicType.kind == thisClassType)
10509                            {
10510                               FreeType(basicType);
10511                               basicType = ReplaceThisClassType(_class);
10512                            }
10513
10514                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10515                            if(tClass.templateClass)
10516                               basicType.passAsTemplate = true;
10517                            */
10518
10519                            FreeType(exp.expType);
10520
10521                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10522                            //exp.expType.refCount++;
10523                            if(!exp.destType)
10524                            {
10525                               exp.destType = exp.expType;
10526                               exp.destType.refCount++;
10527                            }
10528
10529                            {
10530                               Expression newExp { };
10531                               OldList * specs = MkList();
10532                               Declarator decl;
10533                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10534                               *newExp = *exp;
10535                               if(exp.destType) exp.destType.refCount++;
10536                               if(exp.expType)  exp.expType.refCount++;
10537                               exp.type = castExp;
10538                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10539                               exp.cast.exp = newExp;
10540                               //FreeType(exp.expType);
10541                               //exp.expType = null;
10542                               //ProcessExpressionType(sourceExp);
10543                            }
10544                         }
10545                         FinishTemplatesContext(context);
10546                      }
10547                   }
10548                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10549                   {
10550                      Class expClass = exp.expType._class.registered;
10551                      if(expClass)
10552                      {
10553                         Class cClass = null;
10554                         int p = 0;
10555                         int paramCount = 0;
10556                         int lastParam = -1;
10557                         char templateString[1024];
10558                         ClassTemplateParameter param;
10559                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10560                         while(cClass != expClass)
10561                         {
10562                            Class sClass;
10563                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10564                            cClass = sClass;
10565
10566                            for(param = cClass.templateParams.first; param; param = param.next)
10567                            {
10568                               Class cClassCur = null;
10569                               int cp = 0;
10570                               ClassTemplateParameter paramCur = null;
10571                               ClassTemplateArgument arg;
10572                               while(cClassCur != tClass && !paramCur)
10573                               {
10574                                  Class sClassCur;
10575                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10576                                  cClassCur = sClassCur;
10577
10578                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10579                                  {
10580                                     if(!strcmp(paramCur.name, param.name))
10581                                     {
10582
10583                                        break;
10584                                     }
10585                                     cp++;
10586                                  }
10587                               }
10588                               if(paramCur && paramCur.type == TemplateParameterType::type)
10589                                  arg = tClass.templateArgs[cp];
10590                               else
10591                                  arg = expClass.templateArgs[p];
10592
10593                               {
10594                                  char argument[256];
10595                                  argument[0] = '\0';
10596                                  /*if(arg.name)
10597                                  {
10598                                     strcat(argument, arg.name.string);
10599                                     strcat(argument, " = ");
10600                                  }*/
10601                                  switch(param.type)
10602                                  {
10603                                     case expression:
10604                                     {
10605                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10606                                        char expString[1024];
10607                                        OldList * specs = MkList();
10608                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10609                                        Expression exp;
10610                                        char * string = PrintHexUInt64(arg.expression.ui64);
10611                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10612                                        delete string;
10613
10614                                        ProcessExpressionType(exp);
10615                                        ComputeExpression(exp);
10616                                        expString[0] = '\0';
10617                                        PrintExpression(exp, expString);
10618                                        strcat(argument, expString);
10619                                        // delete exp;
10620                                        FreeExpression(exp);
10621                                        break;
10622                                     }
10623                                     case identifier:
10624                                     {
10625                                        strcat(argument, arg.member.name);
10626                                        break;
10627                                     }
10628                                     case TemplateParameterType::type:
10629                                     {
10630                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10631                                           strcat(argument, arg.dataTypeString);
10632                                        break;
10633                                     }
10634                                  }
10635                                  if(argument[0])
10636                                  {
10637                                     if(paramCount) strcat(templateString, ", ");
10638                                     if(lastParam != p - 1)
10639                                     {
10640                                        strcat(templateString, param.name);
10641                                        strcat(templateString, " = ");
10642                                     }
10643                                     strcat(templateString, argument);
10644                                     paramCount++;
10645                                     lastParam = p;
10646                                  }
10647                               }
10648                               p++;
10649                            }
10650                         }
10651                         {
10652                            int len = strlen(templateString);
10653                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10654                            templateString[len++] = '>';
10655                            templateString[len++] = '\0';
10656                         }
10657
10658                         FreeType(exp.expType);
10659                         {
10660                            Context context = SetupTemplatesContext(tClass);
10661                            exp.expType = ProcessTypeString(templateString, false);
10662                            FinishTemplatesContext(context);
10663                         }
10664                      }
10665                   }
10666                }
10667             }
10668             else
10669                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10670          }
10671          else if(type && (type.kind == structType || type.kind == unionType))
10672          {
10673             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10674             if(memberType)
10675             {
10676                exp.expType = memberType;
10677                if(memberType)
10678                   memberType.refCount++;
10679             }
10680          }
10681          else
10682          {
10683             char expString[10240];
10684             expString[0] = '\0';
10685             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10686             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10687          }
10688
10689          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10690          {
10691             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10692             {
10693                Identifier id = exp.member.member;
10694                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10695                if(_class)
10696                {
10697                   FreeType(exp.expType);
10698                   exp.expType = ReplaceThisClassType(_class);
10699                }
10700             }
10701          }
10702          yylloc = oldyylloc;
10703          break;
10704       }
10705       // Convert x->y into (*x).y
10706       case pointerExp:
10707       {
10708          Type destType = exp.destType;
10709
10710          // DOING THIS LATER NOW...
10711          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10712          {
10713             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10714             /* TODO: Name Space Fix ups
10715             if(!exp.member.member.classSym)
10716                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10717             */
10718          }
10719
10720          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10721          exp.type = memberExp;
10722          if(destType)
10723             destType.count++;
10724          ProcessExpressionType(exp);
10725          if(destType)
10726             destType.count--;
10727          break;
10728       }
10729       case classSizeExp:
10730       {
10731          //ComputeExpression(exp);
10732
10733          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10734          if(classSym && classSym.registered)
10735          {
10736             if(classSym.registered.type == noHeadClass || (classSym.registered.fixed && classSym.registered.structSize))
10737             {
10738                char name[1024];
10739                Class b = classSym.registered;
10740                name[0] = '\0';
10741                DeclareStruct(curExternal, classSym.string, false, true);
10742                FreeSpecifier(exp._class);
10743                FullClassNameCat(name, classSym.string, false);
10744
10745                if(b.offset == 0)
10746                {
10747                   exp.type = typeSizeExp;
10748                   exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10749                }
10750                else
10751                {
10752                   Expression e;
10753                   exp.type = opExp;
10754                   if(b.structSize == b.offset)
10755                      exp.op.exp1 = MkExpConstant("0");
10756                   else
10757                      exp.op.exp1 = MkExpTypeSize(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null));
10758                   exp.op.op = '+';
10759                   e = exp;
10760                   while(b.offset != 0)
10761                   {
10762                      Symbol sym;
10763                      Expression typeSize;
10764
10765                      b = b.base;
10766                      sym = FindClass(b.fullName);
10767
10768                      name[0] = '\0';
10769                      DeclareStruct(curExternal, sym.string, false, true);
10770                      FullClassNameCat(name, sym.string, false);
10771
10772                      if(b.structSize == b.offset)
10773                         typeSize = MkExpConstant("0");
10774                      else
10775                         typeSize = MkExpTypeSize(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null));
10776                      e.op.exp2 = b.offset ? MkExpOp(typeSize, '+', null) : typeSize;
10777                      e = e.op.exp2;
10778                   }
10779                }
10780             }
10781             else
10782             {
10783                if(classSym.registered.fixed && !classSym.registered.structSize)
10784                {
10785                   FreeSpecifier(exp._class);
10786                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10787                   exp.type = constantExp;
10788                }
10789                else
10790                {
10791                   char className[1024];
10792                   strcpy(className, "__ecereClass_");
10793                   FullClassNameCat(className, classSym.string, true);
10794
10795                   DeclareClass(curExternal, classSym, className);
10796
10797                   FreeExpContents(exp);
10798                   exp.type = pointerExp;
10799                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10800                   exp.member.member = MkIdentifier("structSize");
10801                }
10802             }
10803          }
10804
10805          exp.expType = Type
10806          {
10807             refCount = 1;
10808             kind = intSizeType;
10809          };
10810          // exp.isConstant = true;
10811          break;
10812       }
10813       case typeSizeExp:
10814       {
10815          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10816
10817          exp.expType = Type
10818          {
10819             refCount = 1;
10820             kind = intSizeType;
10821          };
10822          exp.isConstant = true;
10823
10824          DeclareType(curExternal, type, true, false);
10825          FreeType(type);
10826          break;
10827       }
10828       case castExp:
10829       {
10830          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10831          type.count = 1;
10832          FreeType(exp.cast.exp.destType);
10833          exp.cast.exp.destType = type;
10834          type.refCount++;
10835          type.casted = true;
10836          ProcessExpressionType(exp.cast.exp);
10837          type.casted = false;
10838          type.count = 0;
10839          exp.expType = type;
10840          //type.refCount++;
10841
10842          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10843          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10844          {
10845             void * prev = exp.prev, * next = exp.next;
10846             Type expType = exp.cast.exp.destType;
10847             Expression castExp = exp.cast.exp;
10848             Type destType = exp.destType;
10849
10850             if(expType) expType.refCount++;
10851
10852             //FreeType(exp.destType);
10853             FreeType(exp.expType);
10854             FreeTypeName(exp.cast.typeName);
10855
10856             *exp = *castExp;
10857             FreeType(exp.expType);
10858             FreeType(exp.destType);
10859
10860             exp.expType = expType;
10861             exp.destType = destType;
10862
10863             delete castExp;
10864
10865             exp.prev = prev;
10866             exp.next = next;
10867
10868          }
10869          else
10870          {
10871             exp.isConstant = exp.cast.exp.isConstant;
10872          }
10873          //FreeType(type);
10874          break;
10875       }
10876       case extensionInitializerExp:
10877       {
10878          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10879          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10880          // ProcessInitializer(exp.initializer.initializer, type);
10881          exp.expType = type;
10882          break;
10883       }
10884       case vaArgExp:
10885       {
10886          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10887          ProcessExpressionType(exp.vaArg.exp);
10888          exp.expType = type;
10889          break;
10890       }
10891       case conditionExp:
10892       {
10893          Expression e;
10894          Type t = exp.destType;
10895          if(t && !exp.destType.casted)
10896          {
10897             t = { };
10898             CopyTypeInto(t, exp.destType);
10899             t.count = 0;
10900          }
10901          else if(t)
10902             t.refCount++;
10903
10904          exp.isConstant = true;
10905
10906          FreeType(exp.cond.cond.destType);
10907          exp.cond.cond.destType = MkClassType("bool");
10908          exp.cond.cond.destType.truth = true;
10909          ProcessExpressionType(exp.cond.cond);
10910          if(!exp.cond.cond.isConstant)
10911             exp.isConstant = false;
10912          for(e = exp.cond.exp->first; e; e = e.next)
10913          {
10914             if(!e.next)
10915             {
10916                FreeType(e.destType);
10917                e.destType = t;
10918                if(e.destType) e.destType.refCount++;
10919             }
10920             ProcessExpressionType(e);
10921             if(!e.next)
10922             {
10923                exp.expType = e.expType;
10924                if(e.expType) e.expType.refCount++;
10925             }
10926             if(!e.isConstant)
10927                exp.isConstant = false;
10928          }
10929
10930          FreeType(exp.cond.elseExp.destType);
10931          // Added this check if we failed to find an expType
10932          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10933
10934          // Reversed it...
10935          exp.cond.elseExp.destType = t ? t : exp.expType;
10936
10937          if(exp.cond.elseExp.destType)
10938             exp.cond.elseExp.destType.refCount++;
10939          ProcessExpressionType(exp.cond.elseExp);
10940
10941          // FIXED THIS: Was done before calling process on elseExp
10942          if(!exp.cond.elseExp.isConstant)
10943             exp.isConstant = false;
10944
10945          FreeType(t);
10946          break;
10947       }
10948       case extensionCompoundExp:
10949       {
10950          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10951          {
10952             Statement last = exp.compound.compound.statements->last;
10953             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10954             {
10955                ((Expression)last.expressions->last).destType = exp.destType;
10956                if(exp.destType)
10957                   exp.destType.refCount++;
10958             }
10959             ProcessStatement(exp.compound);
10960             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10961             if(exp.expType)
10962                exp.expType.refCount++;
10963          }
10964          break;
10965       }
10966       case classExp:
10967       {
10968          Specifier spec = exp._classExp.specifiers->first;
10969          if(spec && spec.type == nameSpecifier)
10970          {
10971             exp.expType = MkClassType(spec.name);
10972             exp.expType.kind = subClassType;
10973             exp.byReference = true;
10974          }
10975          else
10976          {
10977             exp.expType = MkClassType("ecere::com::Class");
10978             exp.byReference = true;
10979          }
10980          break;
10981       }
10982       case classDataExp:
10983       {
10984          Class _class = thisClass ? thisClass : currentClass;
10985          if(_class)
10986          {
10987             Identifier id = exp.classData.id;
10988             char structName[1024];
10989             Expression classExp;
10990             strcpy(structName, "__ecereClassData_");
10991             FullClassNameCat(structName, _class.fullName, false);
10992             exp.type = pointerExp;
10993             exp.member.member = id;
10994             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10995                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10996             else
10997                classExp = MkExpIdentifier(MkIdentifier("class"));
10998
10999             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
11000                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
11001                   MkExpBrackets(MkListOne(MkExpOp(
11002                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
11003                         MkExpMember(classExp, MkIdentifier("data"))), '+',
11004                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
11005                      )));
11006
11007             ProcessExpressionType(exp);
11008             return;
11009          }
11010          break;
11011       }
11012       case arrayExp:
11013       {
11014          Type type = null;
11015          const char * typeString = null;
11016          char typeStringBuf[1024];
11017          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
11018             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
11019          {
11020             Class templateClass = exp.destType._class.registered;
11021             typeString = templateClass.templateArgs[2].dataTypeString;
11022          }
11023          else if(exp.list)
11024          {
11025             // Guess type from expressions in the array
11026             Expression e;
11027             for(e = exp.list->first; e; e = e.next)
11028             {
11029                ProcessExpressionType(e);
11030                if(e.expType)
11031                {
11032                   if(!type) { type = e.expType; type.refCount++; }
11033                   else
11034                   {
11035                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11036                      if(!MatchTypeExpression(e, type, null, false, true))
11037                      {
11038                         FreeType(type);
11039                         type = e.expType;
11040                         e.expType = null;
11041
11042                         e = exp.list->first;
11043                         ProcessExpressionType(e);
11044                         if(e.expType)
11045                         {
11046                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
11047                            if(!MatchTypeExpression(e, type, null, false, true))
11048                            {
11049                               FreeType(e.expType);
11050                               e.expType = null;
11051                               FreeType(type);
11052                               type = null;
11053                               break;
11054                            }
11055                         }
11056                      }
11057                   }
11058                   if(e.expType)
11059                   {
11060                      FreeType(e.expType);
11061                      e.expType = null;
11062                   }
11063                }
11064             }
11065             if(type)
11066             {
11067                typeStringBuf[0] = '\0';
11068                PrintTypeNoConst(type, typeStringBuf, false, true);
11069                typeString = typeStringBuf;
11070                FreeType(type);
11071                type = null;
11072             }
11073          }
11074          if(typeString)
11075          {
11076             /*
11077             (Container)& (struct BuiltInContainer)
11078             {
11079                ._vTbl = class(BuiltInContainer)._vTbl,
11080                ._class = class(BuiltInContainer),
11081                .refCount = 0,
11082                .data = (int[]){ 1, 7, 3, 4, 5 },
11083                .count = 5,
11084                .type = class(int),
11085             }
11086             */
11087             char templateString[1024];
11088             OldList * initializers = MkList();
11089             OldList * structInitializers = MkList();
11090             OldList * specs = MkList();
11091             Expression expExt;
11092             Declarator decl = SpecDeclFromString(typeString, specs, null);
11093             sprintf(templateString, "Container<%s>", typeString);
11094
11095             if(exp.list)
11096             {
11097                Expression e;
11098                type = ProcessTypeString(typeString, false);
11099                while((e = exp.list->first))
11100                {
11101                   exp.list->Remove(e);
11102                   e.destType = type;
11103                   type.refCount++;
11104                   ProcessExpressionType(e);
11105                   ListAdd(initializers, MkInitializerAssignment(e));
11106                }
11107                FreeType(type);
11108                delete exp.list;
11109             }
11110
11111             DeclareStruct(curExternal, "ecere::com::BuiltInContainer", false, true);
11112
11113             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
11114                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11115             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
11116                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11117             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
11118                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11119             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
11120                MkTypeName(specs, MkDeclaratorArray(decl, null)),
11121                MkInitializerList(initializers))));
11122                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11123             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
11124                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11125             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
11126                ProcessExpressionType(((Initializer)structInitializers->last).exp);
11127             exp.expType = ProcessTypeString(templateString, false);
11128             exp.type = bracketsExp;
11129             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
11130                MkExpOp(null, '&',
11131                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
11132                   MkInitializerList(structInitializers)))));
11133             ProcessExpressionType(expExt);
11134          }
11135          else
11136          {
11137             exp.expType = ProcessTypeString("Container", false);
11138             Compiler_Error($"Couldn't determine type of array elements\n");
11139          }
11140          break;
11141       }
11142    }
11143
11144    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
11145    {
11146       FreeType(exp.expType);
11147       exp.expType = ReplaceThisClassType(thisClass);
11148    }
11149
11150    // Resolve structures here
11151    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
11152    {
11153       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
11154       // TODO: Fix members reference...
11155       if(symbol)
11156       {
11157          if(exp.expType.kind != enumType)
11158          {
11159             Type member;
11160             String enumName = CopyString(exp.expType.enumName);
11161
11162             // Fixed a memory leak on self-referencing C structs typedefs
11163             // by instantiating a new type rather than simply copying members
11164             // into exp.expType
11165             FreeType(exp.expType);
11166             exp.expType = Type { };
11167             exp.expType.kind = symbol.type.kind;
11168             exp.expType.refCount++;
11169             exp.expType.enumName = enumName;
11170
11171             exp.expType.members = symbol.type.members;
11172             for(member = symbol.type.members.first; member; member = member.next)
11173                member.refCount++;
11174          }
11175          else
11176          {
11177             NamedLink64 member;
11178             for(member = symbol.type.members.first; member; member = member.next)
11179             {
11180                NamedLink64 value { name = CopyString(member.name) };
11181                exp.expType.members.Add(value);
11182             }
11183          }
11184       }
11185    }
11186
11187    // Trying to do this here before conversion properties kick in and this becomes a new expression... (Fixing Class c; const char * a = c;)
11188    // Mark nohead classes as by reference, unless we're casting them to an integral type
11189    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11190       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11191          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11192           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11193    {
11194       exp.byReference = true;
11195    }
11196
11197    yylloc = exp.loc;
11198    if(exp.destType && (/*exp.destType.kind == voidType || */exp.destType.kind == dummyType) );
11199    else if(exp.destType && !exp.destType.keepCast)
11200    {
11201       if(!exp.needTemplateCast && exp.expType && (exp.expType.kind == templateType || exp.expType.passAsTemplate)) // && exp.destType && !exp.destType.passAsTemplate)
11202          exp.needTemplateCast = 1;
11203
11204       if(exp.destType.kind == voidType);
11205       else if(!CheckExpressionType(exp, exp.destType, false, !exp.destType.casted))
11206       {
11207          // Warn for casting unrelated types to/from struct classes
11208          bool invalidCast = false;
11209          if(inCompiler && exp.destType.count && exp.expType)
11210          {
11211             Class c1 = (exp.expType.kind == classType && exp.expType._class) ? exp.expType._class.registered : null;
11212             Class c2 = (exp.destType.kind == classType && exp.destType._class) ? exp.destType._class.registered : null;
11213             if(c1 && c1.type != structClass) c1 = null;
11214             if(c2 && c2.type != structClass) c2 = null;
11215             if((c1 && !exp.expType.byReference && !c2 && !exp.destType.isPointerType) || (c2 && !exp.destType.byReference && !c1 && !exp.expType.isPointerType))
11216                invalidCast = true;
11217          }
11218          if(!exp.destType.count || unresolved || invalidCast)
11219          {
11220             if(!exp.expType)
11221             {
11222                yylloc = exp.loc;
11223                if(exp.destType.kind != ellipsisType)
11224                {
11225                   char type2[1024];
11226                   type2[0] = '\0';
11227                   if(inCompiler)
11228                   {
11229                      char expString[10240];
11230                      expString[0] = '\0';
11231
11232                      PrintType(exp.destType, type2, false, true);
11233
11234                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11235                      if(unresolved)
11236                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
11237                      else if(exp.type != dummyExp)
11238                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
11239                   }
11240                }
11241                else
11242                {
11243                   char expString[10240] ;
11244                   expString[0] = '\0';
11245                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11246
11247                   if(unresolved)
11248                      Compiler_Error($"unresolved identifier %s\n", expString);
11249                   else if(exp.type != dummyExp)
11250                      Compiler_Error($"couldn't determine type of %s\n", expString);
11251                }
11252             }
11253             else
11254             {
11255                char type1[1024];
11256                char type2[1024];
11257                type1[0] = '\0';
11258                type2[0] = '\0';
11259                if(inCompiler)
11260                {
11261                   PrintType(exp.expType, type1, false, true);
11262                   PrintType(exp.destType, type2, false, true);
11263                }
11264
11265                //CheckExpressionType(exp, exp.destType, false);
11266
11267                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
11268                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
11269                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
11270                else
11271                {
11272                   Expression nbExp = GetNonBracketsExp(exp);
11273                   bool skipWarning = false;
11274                   TypeKind kind = exp.destType.kind;
11275                   if(nbExp.type == conditionExp && nbExp.destType && !nbExp.destType.casted && nbExp.destType.kind == exp.destType.kind)
11276                      // The if/else operands have already been checked / warned about
11277                      skipWarning = true;
11278                   if((kind == charType || kind == shortType) && exp.destType.isSigned == exp.expType.signedBeforePromotion && nbExp.type == opExp && nbExp.op.exp1 && nbExp.op.exp2)
11279                   {
11280                      int op = nbExp.op.op;
11281                      Expression nbExp1, nbExp2;
11282                      TypeKind from;
11283
11284                      switch(op)
11285                      {
11286                         case '%': case '/':
11287                            nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11288                            from = nbExp1.expType.promotedFrom;
11289                            // Division and Modulo will not take more room than type before promotion
11290                            if(from == charType || (kind == shortType && from == shortType))
11291                               skipWarning = true;
11292                            break;
11293                         // Left shift
11294                         case LEFT_OP: case RIGHT_OP:
11295                            nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11296                            nbExp2 = GetNonBracketsExp(nbExp.op.exp2);
11297                            from = nbExp1.expType.promotedFrom;
11298                            // Right shift will not take more room than type before promotion
11299                            if(op == RIGHT_OP && (from == charType || (kind == shortType && from == shortType)))
11300                               skipWarning = true;
11301                            else if(nbExp2.isConstant && nbExp2.type == constantExp && (nbExp.op.op == RIGHT_OP || nbExp1.expType.bitMemberSize))
11302                            {
11303                               int n = (int)strtol(nbExp2.constant, null, 0);
11304                               int s = from == charType ? 8 : 16;
11305                               // Left shifting a bit member constrained in size may still fit in type before promotion
11306                               if(nbExp1.expType.bitMemberSize && nbExp1.expType.bitMemberSize < s)
11307                                  s = nbExp1.expType.bitMemberSize;
11308
11309                               // If right shifted enough things will fit in smaller type
11310                               if(nbExp.op.op == RIGHT_OP)
11311                                  s -= n;
11312                               else
11313                                  s += n;
11314                               if(s <= (kind == charType ? 8 : 16))
11315                                  skipWarning = true;
11316                            }
11317                            break;
11318                         case '-':
11319                            if(!exp.destType.isSigned)
11320                            {
11321                               nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11322                               nbExp2 = GetNonBracketsExp(nbExp.op.exp2);
11323                               from = nbExp2.expType.promotedFrom;
11324                               // Max value of unsigned type before promotion minus the same will always fit
11325                               if((from == charType || from == shortType) && nbExp1.isConstant && nbExp1.type == constantExp)
11326                               {
11327                                  int n = (int)strtol(nbExp1.constant, null, 0);
11328                                  if(n == (from == charType ? 255 : 65535))
11329                                     skipWarning = true;
11330                               }
11331                            }
11332                            break;
11333                         case '|':
11334                         {
11335                            TypeKind kind1, kind2;
11336                            nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11337                            nbExp2 = GetNonBracketsExp(nbExp.op.exp2);
11338                            kind1 = nbExp1.expType.promotedFrom ? nbExp1.expType.promotedFrom : nbExp1.expType.kind;
11339                            kind2 = nbExp2.expType.promotedFrom ? nbExp2.expType.promotedFrom : nbExp2.expType.kind;
11340                            if(((kind1 == charType || (kind1 == shortType && kind == shortType)) || MatchTypeExpression(nbExp1, exp.destType, null, false, false)) &&
11341                               ((kind2 == charType || (kind2 == shortType && kind == shortType)) || MatchTypeExpression(nbExp2, exp.destType, null, false, false)))
11342                               skipWarning = true;
11343                            break;
11344                         }
11345                         case '&':
11346                         {
11347                            TypeKind kind1, kind2;
11348                            nbExp1 = GetNonBracketsExp(nbExp.op.exp1);
11349                            nbExp2 = GetNonBracketsExp(nbExp.op.exp2);
11350                            kind1 = nbExp1.expType.promotedFrom ? nbExp1.expType.promotedFrom : nbExp1.expType.kind;
11351                            kind2 = nbExp2.expType.promotedFrom ? nbExp2.expType.promotedFrom : nbExp2.expType.kind;
11352                            if(((kind1 == charType || (kind1 == shortType && kind == shortType)) || MatchTypeExpression(nbExp1, exp.destType, null, false, false)) ||
11353                               ((kind2 == charType || (kind2 == shortType && kind == shortType)) || MatchTypeExpression(nbExp2, exp.destType, null, false, false)))
11354                               skipWarning = true;
11355                            break;
11356                         }
11357                      }
11358                   }
11359
11360                   if(!skipWarning)
11361                   {
11362                      char expString[10240];
11363                      expString[0] = '\0';
11364                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11365
11366 #ifdef _DEBUG
11367                      CheckExpressionType(exp, exp.destType, false, true);
11368 #endif
11369
11370                      // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
11371                      if(!sourceFile || (!strstr(sourceFile, "src\\lexer.ec") && !strstr(sourceFile, "src/lexer.ec") &&
11372                                         !strstr(sourceFile, "src\\grammar.ec") && !strstr(sourceFile, "src/grammar.ec") &&
11373                                         !strstr(sourceFile, "src\\type.ec") && !strstr(sourceFile, "src/type.ec") &&
11374                                         !strstr(sourceFile, "src\\expression.ec") && !strstr(sourceFile, "src/expression.ec")))
11375                      {
11376                         if(invalidCast)
11377                            Compiler_Error($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11378                         else
11379                            Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
11380                      }
11381                   }
11382
11383                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
11384                   if(!inCompiler)
11385                   {
11386                      FreeType(exp.expType);
11387                      exp.destType.refCount++;
11388                      exp.expType = exp.destType;
11389                   }
11390                }
11391             }
11392          }
11393       }
11394       // Cast function pointers to void * as eC already checked compatibility
11395       else if(exp.destType && exp.destType.kind == pointerType && exp.destType.type && exp.destType.type.kind == functionType &&
11396               exp.expType && (exp.expType.kind == functionType || exp.expType.kind == methodType))
11397       {
11398          Expression nbExp = GetNonBracketsExp(exp);
11399          if(nbExp.type != castExp || !IsVoidPtrCast(nbExp.cast.typeName))
11400          {
11401             Expression e = MoveExpContents(exp);
11402             exp.cast.exp = MkExpBrackets(MkListOne(e));
11403             exp.type = castExp;
11404             exp.cast.exp.destType = exp.destType;
11405             if(exp.destType) exp.destType.refCount++;
11406             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
11407          }
11408       }
11409    }
11410    else if(unresolved)
11411    {
11412       if(exp.identifier._class && exp.identifier._class.name)
11413          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
11414       else if(exp.identifier.string && exp.identifier.string[0])
11415          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
11416    }
11417    else if(!exp.expType && exp.type != dummyExp)
11418    {
11419       char expString[10240];
11420       expString[0] = '\0';
11421       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
11422       Compiler_Error($"couldn't determine type of %s\n", expString);
11423    }
11424
11425    // Let's try to support any_object & typed_object here:
11426    if(inCompiler)
11427       ApplyAnyObjectLogic(exp);
11428
11429    // Mark nohead classes as by reference, unless we're casting them to an integral type
11430    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
11431       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
11432          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
11433           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
11434    {
11435       exp.byReference = true;
11436    }
11437    yylloc = oldyylloc;
11438 }
11439
11440 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
11441 {
11442    // THIS CODE WILL FIND NEXT MEMBER...
11443    if(*curMember)
11444    {
11445       *curMember = (*curMember).next;
11446
11447       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
11448       {
11449          *curMember = subMemberStack[--(*subMemberStackPos)];
11450          *curMember = (*curMember).next;
11451       }
11452
11453       // SKIP ALL PROPERTIES HERE...
11454       while((*curMember) && (*curMember).isProperty)
11455          *curMember = (*curMember).next;
11456
11457       if(subMemberStackPos)
11458       {
11459          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11460          {
11461             subMemberStack[(*subMemberStackPos)++] = *curMember;
11462
11463             *curMember = (*curMember).members.first;
11464             while(*curMember && (*curMember).isProperty)
11465                *curMember = (*curMember).next;
11466          }
11467       }
11468    }
11469    while(!*curMember)
11470    {
11471       if(!*curMember)
11472       {
11473          if(subMemberStackPos && *subMemberStackPos)
11474          {
11475             *curMember = subMemberStack[--(*subMemberStackPos)];
11476             *curMember = (*curMember).next;
11477          }
11478          else
11479          {
11480             Class lastCurClass = *curClass;
11481
11482             if(*curClass == _class) break;     // REACHED THE END
11483
11484             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
11485             *curMember = (*curClass).membersAndProperties.first;
11486          }
11487
11488          while((*curMember) && (*curMember).isProperty)
11489             *curMember = (*curMember).next;
11490          if(subMemberStackPos)
11491          {
11492             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
11493             {
11494                subMemberStack[(*subMemberStackPos)++] = *curMember;
11495
11496                *curMember = (*curMember).members.first;
11497                while(*curMember && (*curMember).isProperty)
11498                   *curMember = (*curMember).next;
11499             }
11500          }
11501       }
11502    }
11503 }
11504
11505
11506 static void ProcessInitializer(Initializer init, Type type)
11507 {
11508    switch(init.type)
11509    {
11510       case expInitializer:
11511          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
11512          {
11513             // TESTING THIS FOR SHUTTING = 0 WARNING
11514             if(init.exp && !init.exp.destType)
11515             {
11516                FreeType(init.exp.destType);
11517                init.exp.destType = type;
11518                if(type) type.refCount++;
11519             }
11520             if(init.exp)
11521             {
11522                ProcessExpressionType(init.exp);
11523                init.isConstant = init.exp.isConstant;
11524             }
11525             break;
11526          }
11527          else
11528          {
11529             Expression exp = init.exp;
11530             Instantiation inst = exp.instance;
11531             MembersInit members;
11532
11533             init.type = listInitializer;
11534             init.list = MkList();
11535
11536             if(inst.members)
11537             {
11538                for(members = inst.members->first; members; members = members.next)
11539                {
11540                   if(members.type == dataMembersInit)
11541                   {
11542                      MemberInit member;
11543                      for(member = members.dataMembers->first; member; member = member.next)
11544                      {
11545                         ListAdd(init.list, member.initializer);
11546                         member.initializer = null;
11547                      }
11548                   }
11549                   // Discard all MembersInitMethod
11550                }
11551             }
11552             FreeExpression(exp);
11553          }
11554       case listInitializer:
11555       {
11556          Initializer i;
11557          Type initializerType = null;
11558          Class curClass = null;
11559          DataMember curMember = null;
11560          DataMember subMemberStack[256];
11561          int subMemberStackPos = 0;
11562
11563          if(type && type.kind == arrayType)
11564             initializerType = Dereference(type);
11565          else if(type && (type.kind == structType || type.kind == unionType))
11566             initializerType = type.members.first;
11567
11568          for(i = init.list->first; i; i = i.next)
11569          {
11570             if(type && type.kind == classType && type._class && type._class.registered)
11571             {
11572                // 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)
11573                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
11574                // TODO: Generate error on initializing a private data member this way from another module...
11575                if(curMember)
11576                {
11577                   if(!curMember.dataType)
11578                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
11579                   initializerType = curMember.dataType;
11580                }
11581             }
11582             ProcessInitializer(i, initializerType);
11583             if(initializerType && type && (type.kind == structType || type.kind == unionType))
11584                initializerType = initializerType.next;
11585             if(!i.isConstant)
11586                init.isConstant = false;
11587          }
11588
11589          if(type && type.kind == arrayType)
11590             FreeType(initializerType);
11591
11592          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
11593          {
11594             Compiler_Error($"Assigning list initializer to non list\n");
11595          }
11596          break;
11597       }
11598    }
11599 }
11600
11601 static void ProcessSpecifier(Specifier spec, bool declareStruct, bool warnClasses)
11602 {
11603    switch(spec.type)
11604    {
11605       case baseSpecifier:
11606       {
11607          if(spec.specifier == THISCLASS)
11608          {
11609             if(thisClass)
11610             {
11611                spec.type = nameSpecifier;
11612                spec.name = ReplaceThisClass(thisClass);
11613                spec.symbol = FindClass(spec.name);
11614                ProcessSpecifier(spec, declareStruct, false);
11615             }
11616          }
11617          break;
11618       }
11619       case nameSpecifier:
11620       {
11621          Symbol symbol = FindType(curContext, spec.name);
11622          if(symbol)
11623             DeclareType(curExternal, symbol.type, true, true);
11624          else if(spec.symbol /*&& declareStruct*/)
11625          {
11626             Class c = spec.symbol.registered;
11627             if(warnClasses && !c)
11628                Compiler_Warning("Undeclared class %s\n", spec.name);
11629             DeclareStruct(curExternal, spec.name, c && c.type == noHeadClass, declareStruct && c && c.type == structClass);
11630          }
11631          break;
11632       }
11633       case enumSpecifier:
11634       {
11635          Enumerator e;
11636          if(spec.list)
11637          {
11638             for(e = spec.list->first; e; e = e.next)
11639             {
11640                if(e.exp)
11641                   ProcessExpressionType(e.exp);
11642             }
11643          }
11644          // Fall through for IDE type processing
11645          if(inCompiler)
11646             break;
11647       }
11648       case structSpecifier:
11649       case unionSpecifier:
11650       {
11651          if(spec.definitions)
11652          {
11653             //ClassDef def;
11654             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
11655             //if(symbol)
11656                ProcessClass(spec.definitions, symbol);
11657             /*else
11658             {
11659                for(def = spec.definitions->first; def; def = def.next)
11660                {
11661                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
11662                      ProcessDeclaration(def.decl);
11663                }
11664             }*/
11665          }
11666          break;
11667       }
11668       /*
11669       case classSpecifier:
11670       {
11671          Symbol classSym = FindClass(spec.name);
11672          if(classSym && classSym.registered && classSym.registered.type == structClass)
11673             DeclareStruct(spec.name, false, true);
11674          break;
11675       }
11676       */
11677    }
11678 }
11679
11680
11681 static void ProcessDeclarator(Declarator decl, bool isFunction)
11682 {
11683    switch(decl.type)
11684    {
11685       case identifierDeclarator:
11686          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11687          {
11688             FreeSpecifier(decl.identifier._class);
11689             decl.identifier._class = null;
11690          }
11691          break;
11692       case arrayDeclarator:
11693          if(decl.array.exp)
11694             ProcessExpressionType(decl.array.exp);
11695       case structDeclarator:
11696       case bracketsDeclarator:
11697       case functionDeclarator:
11698       case pointerDeclarator:
11699       case extendedDeclarator:
11700       case extendedDeclaratorEnd:
11701       {
11702          Identifier id = null;
11703          Specifier classSpec = null;
11704          if(decl.type == functionDeclarator)
11705          {
11706             id = GetDeclId(decl);
11707             if(id && id._class)
11708             {
11709                classSpec = id._class;
11710                id._class = null;
11711             }
11712          }
11713          if(decl.declarator)
11714             ProcessDeclarator(decl.declarator, isFunction);
11715          if(decl.type == functionDeclarator)
11716          {
11717             if(classSpec)
11718             {
11719                TypeName param
11720                {
11721                   qualifiers = MkListOne(classSpec);
11722                   declarator = null;
11723                };
11724                if(!decl.function.parameters)
11725                   decl.function.parameters = MkList();
11726                decl.function.parameters->Insert(null, param);
11727             }
11728             if(decl.function.parameters)
11729             {
11730                TypeName param;
11731
11732                for(param = decl.function.parameters->first; param; param = param.next)
11733                {
11734                   if(param.qualifiers)
11735                   {
11736                      Specifier spec;
11737                      for(spec = param.qualifiers->first; spec; spec = spec.next)
11738                      {
11739                         if(spec.type == baseSpecifier)
11740                         {
11741                            if(spec.specifier == TYPED_OBJECT)
11742                            {
11743                               Declarator d = param.declarator;
11744                               TypeName newParam
11745                               {
11746                                  qualifiers = MkListOne(MkSpecifier(VOID));
11747                                  declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11748                               };
11749                               if(!d || d.type != pointerDeclarator)
11750                                  newParam.qualifiers->Insert(null, MkSpecifier(CONST));
11751
11752                               FreeList(param.qualifiers, FreeSpecifier);
11753
11754                               param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11755                               param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11756
11757                               DeclareStruct(curExternal, "ecere::com::Class", false, true);
11758
11759                               decl.function.parameters->Insert(param, newParam);
11760                               param = newParam;
11761                               break;
11762                            }
11763                            else if(spec.specifier == ANY_OBJECT)
11764                            {
11765                               Declarator d = param.declarator;
11766
11767                               FreeList(param.qualifiers, FreeSpecifier);
11768
11769                               param.qualifiers = MkListOne(MkSpecifier(VOID));
11770                               if(!d || d.type != pointerDeclarator)
11771                                  param.qualifiers->Insert(null, MkSpecifier(CONST));
11772                               param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11773                               break;
11774                            }
11775                            else if(spec.specifier == THISCLASS)
11776                            {
11777                               if(thisClass)
11778                               {
11779                                  spec.type = nameSpecifier;
11780                                  spec.name = ReplaceThisClass(thisClass);
11781                                  spec.symbol = FindClass(spec.name);
11782                                  ProcessSpecifier(spec, false, false);
11783                               }
11784                               break;
11785                            }
11786                         }
11787                         else if(spec.type == nameSpecifier)
11788                         {
11789                            ProcessSpecifier(spec, isFunction, true);
11790                         }
11791                         else if((spec.type == structSpecifier || spec.type == unionSpecifier) && !spec.definitions && spec.id && spec.id.string)
11792                         {
11793                            Declarator d = param.declarator;
11794                            if(!d || d.type != pointerDeclarator)
11795                               DeclareStruct(curExternal, spec.id.string, false, true);
11796                         }
11797                      }
11798                   }
11799
11800                   if(param.declarator)
11801                      ProcessDeclarator(param.declarator, false);
11802                }
11803             }
11804          }
11805          break;
11806       }
11807    }
11808 }
11809
11810 static void ProcessDeclaration(Declaration decl, bool warnClasses)
11811 {
11812    yylloc = decl.loc;
11813    switch(decl.type)
11814    {
11815       case initDeclaration:
11816       {
11817          bool declareStruct = false;
11818          /*
11819          lineNum = decl.pos.line;
11820          column = decl.pos.col;
11821          */
11822
11823          if(decl.declarators)
11824          {
11825             InitDeclarator d;
11826
11827             for(d = decl.declarators->first; d; d = d.next)
11828             {
11829                Type type, subType;
11830                ProcessDeclarator(d.declarator, false);
11831
11832                type = ProcessType(decl.specifiers, d.declarator);
11833
11834                if(d.initializer)
11835                {
11836                   ProcessInitializer(d.initializer, type);
11837
11838                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11839
11840                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11841                      d.initializer.exp.type == instanceExp)
11842                   {
11843                      if(type.kind == classType && type._class ==
11844                         d.initializer.exp.expType._class)
11845                      {
11846                         Instantiation inst = d.initializer.exp.instance;
11847                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11848
11849                         d.initializer.exp.instance = null;
11850                         if(decl.specifiers)
11851                            FreeList(decl.specifiers, FreeSpecifier);
11852                         FreeList(decl.declarators, FreeInitDeclarator);
11853
11854                         d = null;
11855
11856                         decl.type = instDeclaration;
11857                         decl.inst = inst;
11858                      }
11859                   }
11860                }
11861                for(subType = type; subType;)
11862                {
11863                   if(subType.kind == classType)
11864                   {
11865                      declareStruct = true;
11866                      break;
11867                   }
11868                   else if(subType.kind == pointerType)
11869                      break;
11870                   else if(subType.kind == arrayType)
11871                      subType = subType.arrayType;
11872                   else
11873                      break;
11874                }
11875
11876                FreeType(type);
11877                if(!d) break;
11878             }
11879          }
11880
11881          if(decl.specifiers)
11882          {
11883             Specifier s;
11884             for(s = decl.specifiers->first; s; s = s.next)
11885             {
11886                ProcessSpecifier(s, declareStruct, true);
11887             }
11888          }
11889          break;
11890       }
11891       case instDeclaration:
11892       {
11893          ProcessInstantiationType(decl.inst);
11894          break;
11895       }
11896       case structDeclaration:
11897       {
11898          Specifier spec;
11899          Declarator d;
11900          bool declareStruct = false;
11901
11902          if(decl.declarators)
11903          {
11904             for(d = decl.declarators->first; d; d = d.next)
11905             {
11906                Type type = ProcessType(decl.specifiers, d.declarator);
11907                Type subType;
11908                ProcessDeclarator(d, false);
11909                for(subType = type; subType;)
11910                {
11911                   if(subType.kind == classType)
11912                   {
11913                      declareStruct = true;
11914                      break;
11915                   }
11916                   else if(subType.kind == pointerType)
11917                      break;
11918                   else if(subType.kind == arrayType)
11919                      subType = subType.arrayType;
11920                   else
11921                      break;
11922                }
11923                FreeType(type);
11924             }
11925          }
11926          if(decl.specifiers)
11927          {
11928             for(spec = decl.specifiers->first; spec; spec = spec.next)
11929                ProcessSpecifier(spec, declareStruct, warnClasses);
11930          }
11931          break;
11932       }
11933    }
11934 }
11935
11936 static FunctionDefinition curFunction;
11937
11938 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11939 {
11940    char propName[1024], propNameM[1024];
11941    char getName[1024], setName[1024];
11942    OldList * args;
11943
11944    DeclareProperty(curExternal, prop, setName, getName);
11945
11946    // eInstance_FireWatchers(object, prop);
11947    strcpy(propName, "__ecereProp_");
11948    FullClassNameCat(propName, prop._class.fullName, false);
11949    strcat(propName, "_");
11950    FullClassNameCat(propName, prop.name, true);
11951
11952    strcpy(propNameM, "__ecerePropM_");
11953    FullClassNameCat(propNameM, prop._class.fullName, false);
11954    strcat(propNameM, "_");
11955    FullClassNameCat(propNameM, prop.name, true);
11956
11957    if(prop.isWatchable)
11958    {
11959       args = MkList();
11960       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11961       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11962       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11963
11964       args = MkList();
11965       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11966       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11967       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11968
11969       DeclareFunctionUtil(curExternal, "eInstance_FireWatchers");
11970    }
11971
11972    {
11973       args = MkList();
11974       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11975       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11976       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11977
11978       args = MkList();
11979       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11980       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11981       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11982
11983       DeclareFunctionUtil(curExternal, "eInstance_FireSelfWatchers");
11984    }
11985
11986    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11987       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11988       curFunction.propSet.fireWatchersDone = true;
11989 }
11990
11991 static void ProcessStatement(Statement stmt)
11992 {
11993    yylloc = stmt.loc;
11994    /*
11995    lineNum = stmt.pos.line;
11996    column = stmt.pos.col;
11997    */
11998    switch(stmt.type)
11999    {
12000       case labeledStmt:
12001          ProcessStatement(stmt.labeled.stmt);
12002          break;
12003       case caseStmt:
12004          // This expression should be constant...
12005          if(stmt.caseStmt.exp)
12006          {
12007             FreeType(stmt.caseStmt.exp.destType);
12008             stmt.caseStmt.exp.destType = curSwitchType;
12009             if(curSwitchType) curSwitchType.refCount++;
12010             ProcessExpressionType(stmt.caseStmt.exp);
12011             ComputeExpression(stmt.caseStmt.exp);
12012          }
12013          if(stmt.caseStmt.stmt)
12014             ProcessStatement(stmt.caseStmt.stmt);
12015          break;
12016       case compoundStmt:
12017       {
12018          if(stmt.compound.context)
12019          {
12020             Declaration decl;
12021             Statement s;
12022
12023             Statement prevCompound = curCompound;
12024             Context prevContext = curContext;
12025
12026             if(!stmt.compound.isSwitch)
12027                curCompound = stmt;
12028             curContext = stmt.compound.context;
12029
12030             if(stmt.compound.declarations)
12031             {
12032                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
12033                   ProcessDeclaration(decl, true);
12034             }
12035             if(stmt.compound.statements)
12036             {
12037                for(s = stmt.compound.statements->first; s; s = s.next)
12038                   ProcessStatement(s);
12039             }
12040
12041             curContext = prevContext;
12042             curCompound = prevCompound;
12043          }
12044          break;
12045       }
12046       case expressionStmt:
12047       {
12048          Expression exp;
12049          if(stmt.expressions)
12050          {
12051             for(exp = stmt.expressions->first; exp; exp = exp.next)
12052                ProcessExpressionType(exp);
12053          }
12054          break;
12055       }
12056       case ifStmt:
12057       {
12058          Expression exp;
12059
12060          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
12061          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
12062          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
12063          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
12064          {
12065             ProcessExpressionType(exp);
12066          }
12067          if(stmt.ifStmt.stmt)
12068             ProcessStatement(stmt.ifStmt.stmt);
12069          if(stmt.ifStmt.elseStmt)
12070             ProcessStatement(stmt.ifStmt.elseStmt);
12071          break;
12072       }
12073       case switchStmt:
12074       {
12075          Type oldSwitchType = curSwitchType;
12076          if(stmt.switchStmt.exp)
12077          {
12078             Expression exp;
12079             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
12080             {
12081                if(!exp.next)
12082                {
12083                   /*
12084                   Type destType
12085                   {
12086                      kind = intType;
12087                      refCount = 1;
12088                   };
12089                   e.exp.destType = destType;
12090                   */
12091
12092                   ProcessExpressionType(exp);
12093                }
12094                if(!exp.next)
12095                   curSwitchType = exp.expType;
12096             }
12097          }
12098          ProcessStatement(stmt.switchStmt.stmt);
12099          curSwitchType = oldSwitchType;
12100          break;
12101       }
12102       case whileStmt:
12103       {
12104          if(stmt.whileStmt.exp)
12105          {
12106             Expression exp;
12107
12108             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
12109             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
12110             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
12111             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
12112             {
12113                ProcessExpressionType(exp);
12114             }
12115          }
12116          if(stmt.whileStmt.stmt)
12117             ProcessStatement(stmt.whileStmt.stmt);
12118          break;
12119       }
12120       case doWhileStmt:
12121       {
12122          if(stmt.doWhile.exp)
12123          {
12124             Expression exp;
12125
12126             if(stmt.doWhile.exp->last)
12127             {
12128                FreeType(((Expression)stmt.doWhile.exp->last).destType);
12129                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
12130                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
12131             }
12132             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
12133             {
12134                ProcessExpressionType(exp);
12135             }
12136          }
12137          if(stmt.doWhile.stmt)
12138             ProcessStatement(stmt.doWhile.stmt);
12139          break;
12140       }
12141       case forStmt:
12142       {
12143          Expression exp;
12144          if(stmt.forStmt.init)
12145             ProcessStatement(stmt.forStmt.init);
12146
12147          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
12148          {
12149             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
12150             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
12151             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
12152          }
12153
12154          if(stmt.forStmt.check)
12155             ProcessStatement(stmt.forStmt.check);
12156          if(stmt.forStmt.increment)
12157          {
12158             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
12159                ProcessExpressionType(exp);
12160          }
12161
12162          if(stmt.forStmt.stmt)
12163             ProcessStatement(stmt.forStmt.stmt);
12164          break;
12165       }
12166       case forEachStmt:
12167       {
12168          Identifier id = stmt.forEachStmt.id;
12169          OldList * exp = stmt.forEachStmt.exp;
12170          OldList * filter = stmt.forEachStmt.filter;
12171          Statement block = stmt.forEachStmt.stmt;
12172          char iteratorType[1024];
12173          Type source;
12174          Expression e;
12175          bool isBuiltin = exp && exp->last &&
12176             (((Expression)exp->last).type == ExpressionType::arrayExp ||
12177               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
12178          Expression arrayExp;
12179          const char * typeString = null;
12180          int builtinCount = 0;
12181
12182          for(e = exp ? exp->first : null; e; e = e.next)
12183          {
12184             if(!e.next)
12185             {
12186                FreeType(e.destType);
12187                e.destType = ProcessTypeString("Container", false);
12188             }
12189             if(!isBuiltin || e.next)
12190                ProcessExpressionType(e);
12191          }
12192
12193          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
12194          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
12195             eClass_IsDerived(source._class.registered, containerClass)))
12196          {
12197             Class _class = source ? source._class.registered : null;
12198             Symbol symbol;
12199             Expression expIt = null;
12200             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false; //, isAVLTree = false;
12201             Class arrayClass = eSystem_FindClass(privateModule, "Array");
12202             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
12203             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
12204
12205             if(inCompiler)
12206             {
12207                stmt.type = compoundStmt;
12208
12209                stmt.compound.context = Context { };
12210                stmt.compound.context.parent = curContext;
12211                curContext = stmt.compound.context;
12212             }
12213
12214             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
12215             {
12216                Class mapClass = eSystem_FindClass(privateModule, "Map");
12217                //Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
12218                isCustomAVLTree = true;
12219                /*if(eClass_IsDerived(source._class.registered, avlTreeClass))
12220                   isAVLTree = true;
12221                else */if(eClass_IsDerived(source._class.registered, mapClass))
12222                   isMap = true;
12223             }
12224             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
12225             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
12226             {
12227                Class listClass = eSystem_FindClass(privateModule, "List");
12228                isLinkList = true;
12229                isList = eClass_IsDerived(source._class.registered, listClass);
12230             }
12231
12232             if(inCompiler && isArray)
12233             {
12234                Declarator decl;
12235                OldList * specs = MkList();
12236                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12237                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12238                stmt.compound.declarations = MkListOne(
12239                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12240                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12241                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
12242                      MkInitializerAssignment(MkExpBrackets(exp))))));
12243             }
12244             else if(isBuiltin)
12245             {
12246                Type type = null;
12247                char typeStringBuf[1024];
12248
12249                // TODO: Merge this code?
12250                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
12251                if(((Expression)exp->last).type == castExp)
12252                {
12253                   TypeName typeName = ((Expression)exp->last).cast.typeName;
12254                   if(typeName)
12255                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
12256                }
12257
12258                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
12259                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
12260                   arrayExp.destType._class.registered.templateArgs)
12261                {
12262                   Class templateClass = arrayExp.destType._class.registered;
12263                   typeString = templateClass.templateArgs[2].dataTypeString;
12264                }
12265                else if(arrayExp.list)
12266                {
12267                   // Guess type from expressions in the array
12268                   Expression e;
12269                   for(e = arrayExp.list->first; e; e = e.next)
12270                   {
12271                      ProcessExpressionType(e);
12272                      if(e.expType)
12273                      {
12274                         if(!type) { type = e.expType; type.refCount++; }
12275                         else
12276                         {
12277                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
12278                            if(!MatchTypeExpression(e, type, null, false, true))
12279                            {
12280                               FreeType(type);
12281                               type = e.expType;
12282                               e.expType = null;
12283
12284                               e = arrayExp.list->first;
12285                               ProcessExpressionType(e);
12286                               if(e.expType)
12287                               {
12288                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
12289                                  if(!MatchTypeExpression(e, type, null, false, true))
12290                                  {
12291                                     FreeType(e.expType);
12292                                     e.expType = null;
12293                                     FreeType(type);
12294                                     type = null;
12295                                     break;
12296                                  }
12297                               }
12298                            }
12299                         }
12300                         if(e.expType)
12301                         {
12302                            FreeType(e.expType);
12303                            e.expType = null;
12304                         }
12305                      }
12306                   }
12307                   if(type)
12308                   {
12309                      typeStringBuf[0] = '\0';
12310                      PrintType(type, typeStringBuf, false, true);
12311                      typeString = typeStringBuf;
12312                      FreeType(type);
12313                   }
12314                }
12315                if(typeString)
12316                {
12317                   if(inCompiler)
12318                   {
12319                      OldList * initializers = MkList();
12320                      Declarator decl;
12321                      OldList * specs = MkList();
12322                      if(arrayExp.list)
12323                      {
12324                         Expression e;
12325
12326                         builtinCount = arrayExp.list->count;
12327                         type = ProcessTypeString(typeString, false);
12328                         while((e = arrayExp.list->first))
12329                         {
12330                            arrayExp.list->Remove(e);
12331                            e.destType = type;
12332                            type.refCount++;
12333                            ProcessExpressionType(e);
12334                            if(inCompiler)
12335                               ListAdd(initializers, MkInitializerAssignment(e));
12336                         }
12337                         FreeType(type);
12338                         delete arrayExp.list;
12339                      }
12340                      decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
12341
12342                      stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
12343                         MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
12344
12345                      ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
12346                         PlugDeclarator(
12347                            /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
12348                            ), MkInitializerList(initializers)))));
12349                      FreeList(exp, FreeExpression);
12350                   }
12351                   else if(arrayExp.list)
12352                   {
12353                      Expression e;
12354                      type = ProcessTypeString(typeString, false);
12355                      for(e = arrayExp.list->first; e; e = e.next)
12356                      {
12357                         e.destType = type;
12358                         type.refCount++;
12359                         ProcessExpressionType(e);
12360                      }
12361                      FreeType(type);
12362                   }
12363                }
12364                else
12365                {
12366                   arrayExp.expType = ProcessTypeString("Container", false);
12367                   Compiler_Error($"Couldn't determine type of array elements\n");
12368                }
12369
12370                /*
12371                Declarator decl;
12372                OldList * specs = MkList();
12373
12374                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
12375                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
12376                stmt.compound.declarations = MkListOne(
12377                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12378                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
12379                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
12380                      MkInitializerAssignment(MkExpBrackets(exp))))));
12381                */
12382             }
12383             else if(inCompiler && isLinkList && !isList)
12384             {
12385                Declarator decl;
12386                OldList * specs = MkList();
12387                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12388                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12389                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12390                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
12391                      MkInitializerAssignment(MkExpBrackets(exp))))));
12392             }
12393             /*else if(isCustomAVLTree)
12394             {
12395                Declarator decl;
12396                OldList * specs = MkList();
12397                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
12398                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
12399                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
12400                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
12401                      MkInitializerAssignment(MkExpBrackets(exp))))));
12402             }*/
12403             else if(inCompiler && _class.templateArgs)
12404             {
12405                if(isMap)
12406                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
12407                else
12408                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
12409
12410                stmt.compound.declarations = MkListOne(
12411                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
12412                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
12413                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
12414             }
12415             if(inCompiler)
12416             {
12417                symbol = FindSymbol(id.string, curContext, curContext, false, false);
12418
12419                if(block)
12420                {
12421                   // Reparent sub-contexts in this statement
12422                   switch(block.type)
12423                   {
12424                      case compoundStmt:
12425                         if(block.compound.context)
12426                            block.compound.context.parent = stmt.compound.context;
12427                         break;
12428                      case ifStmt:
12429                         if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
12430                            block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
12431                         if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
12432                            block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
12433                         break;
12434                      case switchStmt:
12435                         if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
12436                            block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
12437                         break;
12438                      case whileStmt:
12439                         if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
12440                            block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
12441                         break;
12442                      case doWhileStmt:
12443                         if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
12444                            block.doWhile.stmt.compound.context.parent = stmt.compound.context;
12445                         break;
12446                      case forStmt:
12447                         if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
12448                            block.forStmt.stmt.compound.context.parent = stmt.compound.context;
12449                         break;
12450                      case forEachStmt:
12451                         if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
12452                            block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
12453                         break;
12454                      /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
12455                      case labeledStmt:
12456                      case caseStmt
12457                      case expressionStmt:
12458                      case gotoStmt:
12459                      case continueStmt:
12460                      case breakStmt
12461                      case returnStmt:
12462                      case asmStmt:
12463                      case badDeclarationStmt:
12464                      case fireWatchersStmt:
12465                      case stopWatchingStmt:
12466                      case watchStmt:
12467                      */
12468                   }
12469                }
12470
12471                if(filter)
12472                {
12473                   block = MkIfStmt(filter, block, null);
12474                }
12475                if(isArray)
12476                {
12477                   stmt.compound.statements = MkListOne(MkForStmt(
12478                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
12479                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12480                         MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12481                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12482                      block));
12483                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12484                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12485                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12486                }
12487                else if(isBuiltin)
12488                {
12489                   char count[128];
12490                   //OldList * specs = MkList();
12491                   // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12492
12493                   sprintf(count, "%d", builtinCount);
12494
12495                   stmt.compound.statements = MkListOne(MkForStmt(
12496                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
12497                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12498                         MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
12499                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12500                      block));
12501
12502                   /*
12503                   Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
12504                   stmt.compound.statements = MkListOne(MkForStmt(
12505                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
12506                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
12507                         MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
12508                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
12509                      block));
12510                  */
12511                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12512                  ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12513                  ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12514                }
12515                else if(isLinkList && !isList)
12516                {
12517                   Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
12518                   Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
12519                   if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
12520                      !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
12521                   {
12522                      stmt.compound.statements = MkListOne(MkForStmt(
12523                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12524                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12525                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12526                         block));
12527                   }
12528                   else
12529                   {
12530                      OldList * specs = MkList();
12531                      Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
12532                      stmt.compound.statements = MkListOne(MkForStmt(
12533                         MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
12534                         MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12535                         MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
12536                            MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
12537                               MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
12538                         block));
12539                   }
12540                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12541                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12542                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12543                }
12544                /*else if(isCustomAVLTree)
12545                {
12546                   stmt.compound.statements = MkListOne(MkForStmt(
12547                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
12548                         MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
12549                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
12550                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
12551                      block));
12552
12553                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
12554                   ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
12555                   ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
12556                }*/
12557                else
12558                {
12559                   stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
12560                      MkIdentifier("Next")), null)), block));
12561                }
12562                ProcessExpressionType(expIt);
12563                if(stmt.compound.declarations->first)
12564                   ProcessDeclaration(stmt.compound.declarations->first, true);
12565
12566                if(symbol)
12567                   symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
12568
12569                ProcessStatement(stmt);
12570             }
12571             else
12572                ProcessStatement(stmt.forEachStmt.stmt);
12573             if(inCompiler)
12574                curContext = stmt.compound.context.parent;
12575             break;
12576          }
12577          else
12578          {
12579             Compiler_Error($"Expression is not a container\n");
12580          }
12581          break;
12582       }
12583       case gotoStmt:
12584          break;
12585       case continueStmt:
12586          break;
12587       case breakStmt:
12588          break;
12589       case returnStmt:
12590       {
12591          Expression exp;
12592          if(stmt.expressions)
12593          {
12594             for(exp = stmt.expressions->first; exp; exp = exp.next)
12595             {
12596                if(!exp.next)
12597                {
12598                   if(curFunction && !curFunction.type)
12599                      curFunction.type = ProcessType(
12600                         curFunction.specifiers, curFunction.declarator);
12601                   FreeType(exp.destType);
12602                   // TODO: current property if not compiling
12603                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
12604                   if(exp.destType) exp.destType.refCount++;
12605                }
12606                ProcessExpressionType(exp);
12607             }
12608          }
12609          break;
12610       }
12611       case badDeclarationStmt:
12612       {
12613          ProcessDeclaration(stmt.decl, true);
12614          break;
12615       }
12616       case asmStmt:
12617       {
12618          AsmField field;
12619          if(stmt.asmStmt.inputFields)
12620          {
12621             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
12622                if(field.expression)
12623                   ProcessExpressionType(field.expression);
12624          }
12625          if(stmt.asmStmt.outputFields)
12626          {
12627             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
12628                if(field.expression)
12629                   ProcessExpressionType(field.expression);
12630          }
12631          if(stmt.asmStmt.clobberedFields)
12632          {
12633             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
12634             {
12635                if(field.expression)
12636                   ProcessExpressionType(field.expression);
12637             }
12638          }
12639          break;
12640       }
12641       case watchStmt:
12642       {
12643          PropertyWatch propWatch;
12644          OldList * watches = stmt._watch.watches;
12645          Expression object = stmt._watch.object;
12646          Expression watcher = stmt._watch.watcher;
12647          if(watcher)
12648             ProcessExpressionType(watcher);
12649          if(object)
12650             ProcessExpressionType(object);
12651
12652          if(inCompiler)
12653          {
12654             if(watcher || thisClass)
12655             {
12656                External external = curExternal;
12657                Context context = curContext;
12658
12659                stmt.type = expressionStmt;
12660                stmt.expressions = MkList();
12661
12662                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12663                {
12664                   ClassFunction func;
12665                   char watcherName[1024];
12666                   Class watcherClass = watcher ?
12667                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
12668                   External createdExternal;
12669
12670                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
12671                   if(propWatch.deleteWatch)
12672                      strcat(watcherName, "_delete");
12673                   else
12674                   {
12675                      Identifier propID;
12676                      for(propID = propWatch.properties->first; propID; propID = propID.next)
12677                      {
12678                         strcat(watcherName, "_");
12679                         strcat(watcherName, propID.string);
12680                      }
12681                   }
12682
12683                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
12684                   {
12685                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
12686                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
12687                      ProcessClassFunctionBody(func, propWatch.compound);
12688                      propWatch.compound = null;
12689
12690                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
12691
12692                      FreeClassFunction(func);
12693
12694                      curExternal = createdExternal;
12695                      ProcessFunction(createdExternal.function);
12696
12697                      if(propWatch.deleteWatch)
12698                      {
12699                         OldList * args = MkList();
12700                         ListAdd(args, CopyExpression(object));
12701                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12702                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12703                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
12704                      }
12705                      else
12706                      {
12707                         Class _class = object.expType._class.registered;
12708                         Identifier propID;
12709
12710                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12711                         {
12712                            char propName[1024];
12713                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12714                            if(prop)
12715                            {
12716                               char getName[1024], setName[1024];
12717                               OldList * args = MkList();
12718
12719                               DeclareProperty(createdExternal, prop, setName, getName);
12720
12721                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12722                               strcpy(propName, "__ecereProp_");
12723                               FullClassNameCat(propName, prop._class.fullName, false);
12724                               strcat(propName, "_");
12725                               FullClassNameCat(propName, prop.name, true);
12726
12727                               ListAdd(args, CopyExpression(object));
12728                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12729                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12730                               ListAdd(args, MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpIdentifier(MkIdentifier(watcherName))));
12731
12732                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12733
12734                               external.CreateUniqueEdge(createdExternal, true);
12735                            }
12736                            else
12737                               Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12738                         }
12739                      }
12740                   }
12741                   else
12742                      Compiler_Error($"Invalid watched object\n");
12743                }
12744
12745                curExternal = external;
12746                curContext = context;
12747
12748                if(watcher)
12749                   FreeExpression(watcher);
12750                if(object)
12751                   FreeExpression(object);
12752                FreeList(watches, FreePropertyWatch);
12753             }
12754             else
12755                Compiler_Error($"No observer specified and not inside a class\n");
12756          }
12757          else
12758          {
12759             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12760             {
12761                ProcessStatement(propWatch.compound);
12762             }
12763
12764          }
12765          break;
12766       }
12767       case fireWatchersStmt:
12768       {
12769          OldList * watches = stmt._watch.watches;
12770          Expression object = stmt._watch.object;
12771          Class _class;
12772          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12773          // printf("%X\n", watches);
12774          // printf("%X\n", stmt._watch.watches);
12775          if(object)
12776             ProcessExpressionType(object);
12777
12778          if(inCompiler)
12779          {
12780             _class = object ?
12781                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12782
12783             if(_class)
12784             {
12785                Identifier propID;
12786
12787                stmt.type = expressionStmt;
12788                stmt.expressions = MkList();
12789
12790                // Check if we're inside a property set
12791                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12792                {
12793                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12794                }
12795                else if(!watches)
12796                {
12797                   //Compiler_Error($"No property specified and not inside a property set\n");
12798                }
12799                if(watches)
12800                {
12801                   for(propID = watches->first; propID; propID = propID.next)
12802                   {
12803                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12804                      if(prop)
12805                      {
12806                         CreateFireWatcher(prop, object, stmt);
12807                      }
12808                      else
12809                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12810                   }
12811                }
12812                else
12813                {
12814                   // Fire all properties!
12815                   Property prop;
12816                   Class base;
12817                   for(base = _class; base; base = base.base)
12818                   {
12819                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12820                      {
12821                         if(prop.isProperty && prop.isWatchable)
12822                         {
12823                            CreateFireWatcher(prop, object, stmt);
12824                         }
12825                      }
12826                   }
12827                }
12828
12829                if(object)
12830                   FreeExpression(object);
12831                FreeList(watches, FreeIdentifier);
12832             }
12833             else
12834                Compiler_Error($"Invalid object specified and not inside a class\n");
12835          }
12836          break;
12837       }
12838       case stopWatchingStmt:
12839       {
12840          OldList * watches = stmt._watch.watches;
12841          Expression object = stmt._watch.object;
12842          Expression watcher = stmt._watch.watcher;
12843          Class _class;
12844          if(object)
12845             ProcessExpressionType(object);
12846          if(watcher)
12847             ProcessExpressionType(watcher);
12848          if(inCompiler)
12849          {
12850             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12851
12852             if(watcher || thisClass)
12853             {
12854                if(_class)
12855                {
12856                   Identifier propID;
12857
12858                   stmt.type = expressionStmt;
12859                   stmt.expressions = MkList();
12860
12861                   if(!watches)
12862                   {
12863                      OldList * args;
12864                      // eInstance_StopWatching(object, null, watcher);
12865                      args = MkList();
12866                      ListAdd(args, CopyExpression(object));
12867                      ListAdd(args, MkExpConstant("0"));
12868                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12869                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12870                   }
12871                   else
12872                   {
12873                      for(propID = watches->first; propID; propID = propID.next)
12874                      {
12875                         char propName[1024];
12876                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12877                         if(prop)
12878                         {
12879                            char getName[1024], setName[1024];
12880                            OldList * args = MkList();
12881
12882                            DeclareProperty(curExternal, prop, setName, getName);
12883
12884                            // eInstance_StopWatching(object, prop, watcher);
12885                            strcpy(propName, "__ecereProp_");
12886                            FullClassNameCat(propName, prop._class.fullName, false);
12887                            strcat(propName, "_");
12888                            FullClassNameCat(propName, prop.name, true);
12889
12890                            ListAdd(args, CopyExpression(object));
12891                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12892                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12893                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12894                         }
12895                         else
12896                            Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12897                      }
12898                   }
12899
12900                   if(object)
12901                      FreeExpression(object);
12902                   if(watcher)
12903                      FreeExpression(watcher);
12904                   FreeList(watches, FreeIdentifier);
12905                }
12906                else
12907                   Compiler_Error($"Invalid object specified and not inside a class\n");
12908             }
12909             else
12910                Compiler_Error($"No observer specified and not inside a class\n");
12911          }
12912          break;
12913       }
12914    }
12915 }
12916
12917 static void ProcessFunction(FunctionDefinition function)
12918 {
12919    Identifier id = GetDeclId(function.declarator);
12920    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12921    Type type = symbol ? symbol.type : null;
12922    Class oldThisClass = thisClass;
12923    Context oldTopContext = topContext;
12924
12925    yylloc = function.loc;
12926    // Process thisClass
12927
12928    if(type && type.thisClass)
12929    {
12930       Symbol classSym = type.thisClass;
12931       Class _class = type.thisClass.registered;
12932       char className[1024];
12933       char structName[1024];
12934       Declarator funcDecl;
12935       Symbol thisSymbol;
12936
12937       bool typedObject = false;
12938
12939       if(_class && !_class.base)
12940       {
12941          _class = currentClass;
12942          if(_class && !_class.symbol)
12943             _class.symbol = FindClass(_class.fullName);
12944          classSym = _class ? _class.symbol : null;
12945          typedObject = true;
12946       }
12947
12948       thisClass = _class;
12949
12950       if(inCompiler && _class)
12951       {
12952          if(type.kind == functionType)
12953          {
12954             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12955             {
12956                //TypeName param = symbol.type.params.first;
12957                Type param = symbol.type.params.first;
12958                symbol.type.params.Remove(param);
12959                //FreeTypeName(param);
12960                FreeType(param);
12961             }
12962             if(type.classObjectType != classPointer)
12963             {
12964                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12965                symbol.type.staticMethod = true;
12966                symbol.type.thisClass = null;
12967
12968                // HIGH DANGER: VERIFYING THIS...
12969                symbol.type.extraParam = false;
12970             }
12971          }
12972
12973          strcpy(className, "__ecereClass_");
12974          FullClassNameCat(className, _class.fullName, true);
12975
12976          structName[0] = 0;
12977          FullClassNameCat(structName, _class.fullName, false);
12978
12979          // [class] this
12980          funcDecl = GetFuncDecl(function.declarator);
12981          if(funcDecl)
12982          {
12983             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12984             {
12985                TypeName param = funcDecl.function.parameters->first;
12986                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12987                {
12988                   funcDecl.function.parameters->Remove(param);
12989                   FreeTypeName(param);
12990                }
12991             }
12992
12993             if(!function.propertyNoThis)
12994             {
12995                TypeName thisParam = null;
12996
12997                if(type.classObjectType != classPointer)
12998                {
12999                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
13000                   if(!funcDecl.function.parameters)
13001                      funcDecl.function.parameters = MkList();
13002                   funcDecl.function.parameters->Insert(null, thisParam);
13003                }
13004
13005                if(typedObject)
13006                {
13007                   if(type.classObjectType != classPointer)
13008                   {
13009                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
13010                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
13011                   }
13012
13013                   thisParam = TypeName
13014                   {
13015                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
13016                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
13017                   };
13018                   DeclareStruct(curExternal, "ecere::com::Class", false, true);
13019                   funcDecl.function.parameters->Insert(null, thisParam);
13020                }
13021             }
13022          }
13023
13024          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
13025          {
13026             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
13027             funcDecl = GetFuncDecl(initDecl.declarator);
13028             if(funcDecl)
13029             {
13030                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
13031                {
13032                   TypeName param = funcDecl.function.parameters->first;
13033                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
13034                   {
13035                      funcDecl.function.parameters->Remove(param);
13036                      FreeTypeName(param);
13037                   }
13038                }
13039
13040                if(type.classObjectType != classPointer)
13041                {
13042                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
13043                   {
13044                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
13045
13046                      if(!funcDecl.function.parameters)
13047                         funcDecl.function.parameters = MkList();
13048                      funcDecl.function.parameters->Insert(null, thisParam);
13049                   }
13050                }
13051             }
13052          }
13053       }
13054
13055       // Add this to the context
13056       if(function.body)
13057       {
13058          if(type.classObjectType != classPointer)
13059          {
13060             thisSymbol = Symbol
13061             {
13062                string = CopyString("this");
13063                type = classSym ? MkClassType(classSym.string) : null;
13064             };
13065             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
13066
13067             if(typedObject && thisSymbol.type)
13068             {
13069                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
13070                thisSymbol.type.byReference = type.byReference;
13071                thisSymbol.type.typedByReference = type.byReference;
13072             }
13073          }
13074       }
13075
13076       // Pointer to class data
13077       if(inCompiler && _class && _class.type == normalClass && type.classObjectType != classPointer)
13078       {
13079          DataMember member = null;
13080          {
13081             Class base;
13082             for(base = _class; base && base.type != systemClass; base = base.next)
13083             {
13084                for(member = base.membersAndProperties.first; member; member = member.next)
13085                   if(!member.isProperty)
13086                      break;
13087                if(member)
13088                   break;
13089             }
13090          }
13091          for(member = _class.membersAndProperties.first; member; member = member.next)
13092             if(!member.isProperty)
13093                break;
13094          if(member)
13095          {
13096             char pointerName[1024];
13097
13098             Declaration decl;
13099             Initializer initializer;
13100             Expression exp, bytePtr;
13101
13102             strcpy(pointerName, "__ecerePointer_");
13103             FullClassNameCat(pointerName, _class.fullName, false);
13104             {
13105                char className[1024];
13106                strcpy(className, "__ecereClass_");
13107                FullClassNameCat(className, classSym.string, true);
13108
13109                DeclareClass(curExternal, classSym, className);
13110             }
13111
13112             // ((byte *) this)
13113             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
13114
13115             if(_class.fixed)
13116             {
13117                Expression e;
13118                if(_class.offset && _class.offset == (_class.base.type == noHeadClass ? _class.base.memberOffset : _class.base.structSize))
13119                {
13120                   e = MkExpClassSize(MkSpecifierName(_class.base.fullName));
13121                   ProcessExpressionType(e);
13122                }
13123                else
13124                {
13125                   char string[256];
13126                   sprintf(string, "%d", _class.offset);  // Need Bootstrap Fix
13127                   e = MkExpConstant(string);
13128                }
13129                exp = QBrackets(MkExpOp(bytePtr, '+', e));
13130             }
13131             else
13132             {
13133                // ([bytePtr] + [className]->offset)
13134                exp = QBrackets(MkExpOp(bytePtr, '+',
13135                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
13136             }
13137
13138             // (this ? [exp] : 0)
13139             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
13140             exp.expType = Type
13141             {
13142                refCount = 1;
13143                kind = pointerType;
13144                type = Type { refCount = 1, kind = voidType };
13145             };
13146
13147             if(function.body)
13148             {
13149                yylloc = function.body.loc;
13150                // ([structName] *) [exp]
13151                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
13152                initializer = MkInitializerAssignment(
13153                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
13154
13155                // [structName] * [pointerName] = [initializer];
13156                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
13157
13158                {
13159                   Context prevContext = curContext;
13160                   OldList * list;
13161                   curContext = function.body.compound.context;
13162
13163                   decl = MkDeclaration((list = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null))),
13164                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
13165                   list->Insert(null, MkSpecifierExtended(MkExtDeclAttrib(MkAttrib(ATTRIB, MkListOne(MkAttribute(CopyString("unused"), null))))));
13166
13167                   curContext = prevContext;
13168                }
13169
13170                // WHY?
13171                decl.symbol = null;
13172
13173                if(!function.body.compound.declarations)
13174                   function.body.compound.declarations = MkList();
13175                function.body.compound.declarations->Insert(null, decl);
13176             }
13177          }
13178       }
13179
13180
13181       // Loop through the function and replace undeclared identifiers
13182       // which are a member of the class (methods, properties or data)
13183       // by "this.[member]"
13184    }
13185    else
13186       thisClass = null;
13187
13188    if(id)
13189    {
13190       FreeSpecifier(id._class);
13191       id._class = null;
13192
13193       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
13194       {
13195          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
13196          id = GetDeclId(initDecl.declarator);
13197
13198          FreeSpecifier(id._class);
13199          id._class = null;
13200       }
13201    }
13202    if(function.body)
13203       topContext = function.body.compound.context;
13204    {
13205       FunctionDefinition oldFunction = curFunction;
13206       curFunction = function;
13207       if(function.body)
13208          ProcessStatement(function.body);
13209
13210       // If this is a property set and no firewatchers has been done yet, add one here
13211       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
13212       {
13213          Statement prevCompound = curCompound;
13214          Context prevContext = curContext;
13215
13216          Statement fireWatchers = MkFireWatchersStmt(null, null);
13217          if(!function.body.compound.statements) function.body.compound.statements = MkList();
13218          ListAdd(function.body.compound.statements, fireWatchers);
13219
13220          curCompound = function.body;
13221          curContext = function.body.compound.context;
13222
13223          ProcessStatement(fireWatchers);
13224
13225          curContext = prevContext;
13226          curCompound = prevCompound;
13227
13228       }
13229
13230       curFunction = oldFunction;
13231    }
13232
13233    if(function.declarator)
13234    {
13235       ProcessDeclarator(function.declarator, true);
13236    }
13237
13238    topContext = oldTopContext;
13239    thisClass = oldThisClass;
13240 }
13241
13242 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
13243 static void ProcessClass(OldList definitions, Symbol symbol)
13244 {
13245    ClassDef def;
13246    External external = curExternal;
13247    Class regClass = symbol ? symbol.registered : null;
13248
13249    // Process all functions
13250    for(def = definitions.first; def; def = def.next)
13251    {
13252       if(def.type == functionClassDef)
13253       {
13254          if(def.function.declarator)
13255             curExternal = def.function.declarator.symbol.pointerExternal;
13256          else
13257             curExternal = external;
13258
13259          ProcessFunction((FunctionDefinition)def.function);
13260       }
13261       else if(def.type == declarationClassDef)
13262       {
13263          if(def.decl.type == instDeclaration)
13264          {
13265             thisClass = regClass;
13266             ProcessInstantiationType(def.decl.inst);
13267             thisClass = null;
13268          }
13269          // Testing this
13270          else
13271          {
13272             Class backThisClass = thisClass;
13273             if(regClass) thisClass = regClass;
13274             ProcessDeclaration(def.decl, symbol ? true : false);
13275             thisClass = backThisClass;
13276          }
13277       }
13278       else if(def.type == defaultPropertiesClassDef && def.defProperties)
13279       {
13280          MemberInit defProperty;
13281
13282          // Add this to the context
13283          Symbol thisSymbol = Symbol
13284          {
13285             string = CopyString("this");
13286             type = regClass ? MkClassType(regClass.fullName) : null;
13287          };
13288          globalContext.symbols.Add((BTNode)thisSymbol);
13289
13290          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
13291          {
13292             thisClass = regClass;
13293             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
13294             thisClass = null;
13295          }
13296
13297          globalContext.symbols.Remove((BTNode)thisSymbol);
13298          FreeSymbol(thisSymbol);
13299       }
13300       else if(def.type == propertyClassDef && def.propertyDef)
13301       {
13302          PropertyDef prop = def.propertyDef;
13303
13304          // Add this to the context
13305          thisClass = regClass;
13306          if(prop.setStmt)
13307          {
13308             if(regClass)
13309             {
13310                Symbol thisSymbol
13311                {
13312                   string = CopyString("this");
13313                   type = MkClassType(regClass.fullName);
13314                };
13315                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13316             }
13317
13318             curExternal = prop.symbol ? prop.symbol.externalSet : null;
13319             ProcessStatement(prop.setStmt);
13320          }
13321          if(prop.getStmt)
13322          {
13323             if(regClass)
13324             {
13325                Symbol thisSymbol
13326                {
13327                   string = CopyString("this");
13328                   type = MkClassType(regClass.fullName);
13329                };
13330                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13331             }
13332
13333             curExternal = prop.symbol ? prop.symbol.externalGet : null;
13334             ProcessStatement(prop.getStmt);
13335          }
13336          if(prop.issetStmt)
13337          {
13338             if(regClass)
13339             {
13340                Symbol thisSymbol
13341                {
13342                   string = CopyString("this");
13343                   type = MkClassType(regClass.fullName);
13344                };
13345                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
13346             }
13347
13348             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
13349             ProcessStatement(prop.issetStmt);
13350          }
13351
13352          thisClass = null;
13353       }
13354       else if(def.type == propertyWatchClassDef && def.propertyWatch)
13355       {
13356          PropertyWatch propertyWatch = def.propertyWatch;
13357
13358          thisClass = regClass;
13359          if(propertyWatch.compound)
13360          {
13361             Symbol thisSymbol
13362             {
13363                string = CopyString("this");
13364                type = regClass ? MkClassType(regClass.fullName) : null;
13365             };
13366
13367             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
13368
13369             curExternal = null;
13370             ProcessStatement(propertyWatch.compound);
13371          }
13372          thisClass = null;
13373       }
13374    }
13375 }
13376
13377 void DeclareFunctionUtil(External neededBy, const String s)
13378 {
13379    GlobalFunction function = eSystem_FindFunction(privateModule, s);
13380    if(function)
13381    {
13382       char name[1024];
13383       name[0] = 0;
13384       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
13385          strcpy(name, "__ecereFunction_");
13386       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
13387       DeclareFunction(neededBy, function, name);
13388    }
13389    else if(neededBy)
13390       FindSymbol(s, globalContext, globalContext, false, false);
13391 }
13392
13393 bool reachedPass15;
13394
13395 void ComputeDataTypes()
13396 {
13397    External external;
13398
13399    currentClass = null;
13400
13401    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
13402
13403    DeclareStruct(null, "ecere::com::Class", false, true);
13404    DeclareStruct(null, "ecere::com::Instance", false, true);
13405    DeclareStruct(null, "ecere::com::Property", false, true);
13406    DeclareStruct(null, "ecere::com::DataMember", false, true);
13407    DeclareStruct(null, "ecere::com::Method", false, true);
13408    DeclareStruct(null, "ecere::com::SerialBuffer", false, true);
13409    DeclareStruct(null, "ecere::com::ClassTemplateArgument", false, true);
13410
13411    DeclareFunctionUtil(null, "eSystem_New");
13412    DeclareFunctionUtil(null, "eSystem_New0");
13413    DeclareFunctionUtil(null, "eSystem_Renew");
13414    DeclareFunctionUtil(null, "eSystem_Renew0");
13415    DeclareFunctionUtil(null, "eSystem_Delete");
13416    DeclareFunctionUtil(null, "eClass_GetProperty");
13417    DeclareFunctionUtil(null, "eClass_SetProperty");
13418    DeclareFunctionUtil(null, "eInstance_FireSelfWatchers");
13419    DeclareFunctionUtil(null, "eInstance_SetMethod");
13420    DeclareFunctionUtil(null, "eInstance_IncRef");
13421    DeclareFunctionUtil(null, "eInstance_StopWatching");
13422    DeclareFunctionUtil(null, "eInstance_Watch");
13423    DeclareFunctionUtil(null, "eInstance_FireWatchers");
13424    reachedPass15 = true;
13425
13426    for(external = ast->first; external; external = external.next)
13427    {
13428       afterExternal = curExternal = external;
13429       if(external.type == functionExternal)
13430       {
13431          if(memoryGuard)
13432          {
13433             DeclareFunctionUtil(external, "MemoryGuard_PushLoc");
13434             DeclareFunctionUtil(external, "MemoryGuard_PopLoc");
13435          }
13436
13437          currentClass = external.function._class;
13438          ProcessFunction(external.function);
13439       }
13440       // There shouldn't be any _class member access here anyways...
13441       else if(external.type == declarationExternal)
13442       {
13443          if(memoryGuard && external.declaration && external.declaration.type == instDeclaration)
13444          {
13445             DeclareFunctionUtil(external, "MemoryGuard_PushLoc");
13446             DeclareFunctionUtil(external, "MemoryGuard_PopLoc");
13447          }
13448
13449          currentClass = null;
13450          if(external.declaration)
13451             ProcessDeclaration(external.declaration, true);
13452       }
13453       else if(external.type == classExternal)
13454       {
13455          ClassDefinition _class = external._class;
13456          currentClass = external.symbol.registered;
13457          if(memoryGuard)
13458          {
13459             DeclareFunctionUtil(external, "MemoryGuard_PushLoc");
13460             DeclareFunctionUtil(external, "MemoryGuard_PopLoc");
13461          }
13462          if(_class.definitions)
13463          {
13464             ProcessClass(_class.definitions, _class.symbol);
13465          }
13466          if(inCompiler)
13467          {
13468             // Free class data...
13469             ast->Remove(external);
13470             delete external;
13471          }
13472       }
13473       else if(external.type == nameSpaceExternal)
13474       {
13475          thisNameSpace = external.id.string;
13476       }
13477    }
13478    currentClass = null;
13479    thisNameSpace = null;
13480    curExternal = null;
13481 }