pdecl.pas 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Does declaration parsing for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecl;
  19. interface
  20. uses
  21. globtype,tokens,globals,symtable;
  22. var
  23. { pointer to the last read type symbol, (for "forward" }
  24. { types) }
  25. lasttypesym : ptypesym;
  26. { hack, which allows to use the current parsed }
  27. { object type as function argument type }
  28. testcurobject : byte;
  29. curobjectname : stringid;
  30. { reads a string type with optional length }
  31. { and returns a pointer to the string }
  32. { definition }
  33. function stringtype : pdef;
  34. { reads a string, file type or a type id and returns a name and }
  35. { pdef }
  36. function single_type(var s : string) : pdef;
  37. { reads the declaration blocks }
  38. procedure read_declarations(islibrary : boolean);
  39. { reads declarations in the interface part of a unit }
  40. procedure read_interface_declarations;
  41. implementation
  42. uses
  43. cobjects,scanner,aasm,tree,pass_1,
  44. files,types,hcodegen,verbose,systems,import
  45. {$ifdef GDB}
  46. ,gdb
  47. {$endif GDB}
  48. { parser specific stuff }
  49. ,pbase,ptconst,pexpr,psub,pexports
  50. { processor specific stuff }
  51. {$ifdef i386}
  52. ,i386
  53. {$endif}
  54. {$ifdef m68k}
  55. ,m68k
  56. {$endif}
  57. ;
  58. function read_type(const name : stringid) : pdef;forward;
  59. { search in symtablestack used, but not defined type }
  60. procedure testforward_type(p : psym);{$ifndef FPC}far;{$endif}
  61. var
  62. reaktvarsymtable : psymtable;
  63. oldaktfilepos : tfileposinfo;
  64. begin
  65. if not(p^.typ=typesym) then
  66. exit;
  67. if ((p^.properties and sp_forwarddef)<>0) then
  68. begin
  69. oldaktfilepos:=aktfilepos;
  70. aktfilepos:=p^.fileinfo;
  71. Message1(sym_e_forward_type_not_resolved,p^.name);
  72. aktfilepos:=oldaktfilepos;
  73. { try to recover }
  74. ptypesym(p)^.definition:=generrordef;
  75. p^.properties:=p^.properties and (not sp_forwarddef);
  76. end
  77. else
  78. if (ptypesym(p)^.definition^.deftype in [recorddef,objectdef]) then
  79. begin
  80. if (ptypesym(p)^.definition^.deftype=recorddef) then
  81. reaktvarsymtable:=precdef(ptypesym(p)^.definition)^.symtable
  82. else
  83. reaktvarsymtable:=pobjectdef(ptypesym(p)^.definition)^.publicsyms;
  84. {$ifdef tp}
  85. reaktvarsymtable^.foreach(testforward_type);
  86. {$else}
  87. reaktvarsymtable^.foreach(@testforward_type);
  88. {$endif}
  89. end;
  90. end;
  91. procedure const_dec;
  92. var
  93. name : stringid;
  94. p : ptree;
  95. def : pdef;
  96. sym : psym;
  97. storetokenpos,filepos : tfileposinfo;
  98. old_block_type : tblock_type;
  99. ps : pconstset;
  100. pd : pbestreal;
  101. sp : pstring;
  102. l : longint;
  103. begin
  104. consume(_CONST);
  105. old_block_type:=block_type;
  106. block_type:=bt_const;
  107. repeat
  108. name:=pattern;
  109. filepos:=tokenpos;
  110. consume(ID);
  111. case token of
  112. EQUAL:
  113. begin
  114. consume(EQUAL);
  115. p:=comp_expr(true);
  116. do_firstpass(p);
  117. storetokenpos:=tokenpos;
  118. tokenpos:=filepos;
  119. case p^.treetype of
  120. ordconstn:
  121. begin
  122. if is_constintnode(p) then
  123. symtablestack^.insert(new(pconstsym,init(name,constint,p^.value,nil)))
  124. else if is_constcharnode(p) then
  125. symtablestack^.insert(new(pconstsym,init(name,constchar,p^.value,nil)))
  126. else if is_constboolnode(p) then
  127. symtablestack^.insert(new(pconstsym,init(name,constbool,p^.value,nil)))
  128. else if p^.resulttype^.deftype=enumdef then
  129. symtablestack^.insert(new(pconstsym,init(name,constord,p^.value,p^.resulttype)))
  130. else if p^.resulttype^.deftype=pointerdef then
  131. symtablestack^.insert(new(pconstsym,init(name,constord,p^.value,p^.resulttype)))
  132. else internalerror(111);
  133. end;
  134. stringconstn:
  135. begin
  136. if p^.length>255 then
  137. l:=255
  138. else
  139. l:=p^.length;
  140. { value_str is disposed with p so I need a copy }
  141. getmem(sp,l+1);
  142. move(p^.value_str^,sp^[1],l);
  143. {$ifndef TP}
  144. {$ifopt H+}
  145. setlength(sp^,l);
  146. {$else}
  147. sp^[0]:=chr(l);
  148. {$endif}
  149. {$else}
  150. sp^[0]:=chr(l);
  151. {$endif}
  152. symtablestack^.insert(new(pconstsym,init(name,conststring,longint(sp),nil)));
  153. end;
  154. realconstn :
  155. begin
  156. new(pd);
  157. pd^:=p^.value_real;
  158. symtablestack^.insert(new(pconstsym,init(name,constreal,longint(pd),nil)));
  159. end;
  160. setconstn :
  161. begin
  162. new(ps);
  163. ps^:=p^.value_set^;
  164. symtablestack^.insert(new(pconstsym,init(name,constset,longint(ps),p^.resulttype)));
  165. end;
  166. niln :
  167. begin
  168. symtablestack^.insert(new(pconstsym,init(name,constnil,0,p^.resulttype)));
  169. end;
  170. else
  171. Message(cg_e_illegal_expression);
  172. end;
  173. tokenpos:=storetokenpos;
  174. consume(SEMICOLON);
  175. disposetree(p);
  176. end;
  177. COLON:
  178. begin
  179. { set the blocktype first so a consume also supports a
  180. caret, to support const s : ^string = nil }
  181. block_type:=bt_type;
  182. consume(COLON);
  183. ignore_equal:=true;
  184. def:=read_type('');
  185. ignore_equal:=false;
  186. block_type:=bt_const;
  187. storetokenpos:=tokenpos;
  188. tokenpos:=filepos;
  189. sym:=new(ptypedconstsym,init(name,def));
  190. tokenpos:=storetokenpos;
  191. symtablestack^.insert(sym);
  192. consume(EQUAL);
  193. readtypedconst(def,ptypedconstsym(sym));
  194. consume(SEMICOLON);
  195. end;
  196. else consume(EQUAL);
  197. end;
  198. until token<>ID;
  199. block_type:=old_block_type;
  200. end;
  201. procedure label_dec;
  202. var
  203. hl : plabel;
  204. begin
  205. consume(_LABEL);
  206. if not(cs_support_goto in aktmoduleswitches) then
  207. Message(sym_e_goto_and_label_not_supported);
  208. repeat
  209. if not(token in [ID,INTCONST]) then
  210. consume(ID)
  211. else
  212. begin
  213. getlabel(hl);
  214. symtablestack^.insert(new(plabelsym,init(pattern,hl)));
  215. consume(token);
  216. end;
  217. if token<>SEMICOLON then consume(COMMA);
  218. until not(token in [ID,INTCONST]);
  219. consume(SEMICOLON);
  220. end;
  221. procedure read_var_decs(is_record,is_object:boolean);
  222. { reads the filed of a record into a }
  223. { symtablestack, if record=false }
  224. { variants are forbidden, so this procedure }
  225. { can be used to read object fields }
  226. { if absolute is true, ABSOLUTE and file }
  227. { types are allowed }
  228. { => the procedure is also used to read }
  229. { a sequence of variable declaration }
  230. var
  231. sc : pstringcontainer;
  232. s : stringid;
  233. old_block_type : tblock_type;
  234. declarepos,storetokenpos : tfileposinfo;
  235. symdone : boolean;
  236. { to handle absolute }
  237. abssym : pabsolutesym;
  238. l : longint;
  239. code : word;
  240. { c var }
  241. newtype : ptypesym;
  242. is_dll,
  243. is_gpc_name,is_cdecl,extern_aktvarsym,export_aktvarsym : boolean;
  244. dll_name,
  245. C_name : string;
  246. { case }
  247. p,casedef : pdef;
  248. { maxsize contains the max. size of a variant }
  249. { startvarrec contains the start of the variant part of a record }
  250. maxsize,startvarrec : longint;
  251. pt : ptree;
  252. begin
  253. old_block_type:=block_type;
  254. block_type:=bt_type;
  255. is_gpc_name:=false;
  256. { Force an expected ID error message }
  257. if not (token in [ID,_CASE,_END]) then
  258. consume(ID);
  259. { read vars }
  260. while (token=ID) and
  261. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  262. begin
  263. C_name:=orgpattern;
  264. sc:=idlist;
  265. consume(COLON);
  266. if (m_gpc in aktmodeswitches) and
  267. not(is_record or is_object) and
  268. (token=ID) and (orgpattern='__asmname__') then
  269. begin
  270. consume(ID);
  271. C_name:=pattern;
  272. if token=CCHAR then
  273. consume(CCHAR)
  274. else
  275. consume(CSTRING);
  276. Is_gpc_name:=true;
  277. end;
  278. p:=read_type('');
  279. symdone:=false;
  280. if is_gpc_name then
  281. begin
  282. storetokenpos:=tokenpos;
  283. s:=sc^.get_with_tokeninfo(tokenpos);
  284. if not sc^.empty then
  285. Message(parser_e_absolute_only_one_var);
  286. dispose(sc,done);
  287. aktvarsym:=new(pvarsym,init_C(s,target_os.Cprefix+C_name,p));
  288. tokenpos:=storetokenpos;
  289. aktvarsym^.var_options:=aktvarsym^.var_options or vo_is_external;
  290. externals^.concat(new(pai_external,init(aktvarsym^.mangledname,EXT_NEAR)));
  291. symtablestack^.insert(aktvarsym);
  292. symdone:=true;
  293. end;
  294. { check for absolute }
  295. if not symdone and
  296. (idtoken=_ABSOLUTE) and not(is_record or is_object) then
  297. begin
  298. consume(_ABSOLUTE);
  299. { only allowed for one var }
  300. s:=sc^.get_with_tokeninfo(declarepos);
  301. if not sc^.empty then
  302. Message(parser_e_absolute_only_one_var);
  303. dispose(sc,done);
  304. { parse the rest }
  305. if token=ID then
  306. begin
  307. getsym(pattern,true);
  308. consume(ID);
  309. { we should check the result type of srsym }
  310. if not (srsym^.typ in [varsym,typedconstsym]) then
  311. Message(parser_e_absolute_only_to_var_or_const);
  312. storetokenpos:=tokenpos;
  313. tokenpos:=declarepos;
  314. abssym:=new(pabsolutesym,init(s,p));
  315. abssym^.abstyp:=tovar;
  316. abssym^.ref:=srsym;
  317. tokenpos:=storetokenpos;
  318. symtablestack^.insert(abssym);
  319. end
  320. else
  321. if (token=CSTRING) or (token=CCHAR) then
  322. begin
  323. storetokenpos:=tokenpos;
  324. tokenpos:=declarepos;
  325. abssym:=new(pabsolutesym,init(s,p));
  326. s:=pattern;
  327. consume(token);
  328. abssym^.abstyp:=toasm;
  329. abssym^.asmname:=stringdup(s);
  330. tokenpos:=storetokenpos;
  331. symtablestack^.insert(abssym);
  332. end
  333. else
  334. { absolute address ?!? }
  335. if token=INTCONST then
  336. begin
  337. if (target_info.target=target_i386_go32v2) then
  338. begin
  339. storetokenpos:=tokenpos;
  340. tokenpos:=declarepos;
  341. abssym:=new(pabsolutesym,init(s,p));
  342. abssym^.abstyp:=toaddr;
  343. abssym^.absseg:=false;
  344. tokenpos:=storetokenpos;
  345. s:=pattern;
  346. consume(INTCONST);
  347. val(s,abssym^.address,code);
  348. if token=COLON then
  349. begin
  350. consume(token);
  351. s:=pattern;
  352. consume(INTCONST);
  353. val(s,l,code);
  354. abssym^.address:=abssym^.address shl 4+l;
  355. abssym^.absseg:=true;
  356. end;
  357. symtablestack^.insert(abssym);
  358. end
  359. else
  360. Message(parser_e_absolute_only_to_var_or_const);
  361. end
  362. else
  363. Message(parser_e_absolute_only_to_var_or_const);
  364. symdone:=true;
  365. end;
  366. { for a record there doesn't need to be a ; before the END or ) }
  367. if not((is_record or is_object) and (token in [_END,RKLAMMER])) then
  368. consume(SEMICOLON);
  369. { procvar handling }
  370. if (p^.deftype=procvardef) and (p^.sym=nil) then
  371. begin
  372. newtype:=new(ptypesym,init('unnamed',p));
  373. parse_var_proc_directives(newtype);
  374. newtype^.definition:=nil;
  375. p^.sym:=nil;
  376. dispose(newtype,done);
  377. end;
  378. { Check for variable directives }
  379. if not symdone and (token=ID) then
  380. begin
  381. { Check for C Variable declarations }
  382. if (m_cvar_support in aktmodeswitches) and
  383. not(is_record or is_object) and
  384. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  385. begin
  386. { only allowed for one var }
  387. s:=sc^.get_with_tokeninfo(declarepos);
  388. if not sc^.empty then
  389. Message(parser_e_absolute_only_one_var);
  390. dispose(sc,done);
  391. { defaults }
  392. is_dll:=false;
  393. is_cdecl:=false;
  394. extern_aktvarsym:=false;
  395. export_aktvarsym:=false;
  396. { cdecl }
  397. if idtoken=_CVAR then
  398. begin
  399. consume(_CVAR);
  400. consume(SEMICOLON);
  401. is_cdecl:=true;
  402. C_name:=target_os.Cprefix+C_name;
  403. end;
  404. { external }
  405. if idtoken=_EXTERNAL then
  406. begin
  407. consume(_EXTERNAL);
  408. extern_aktvarsym:=true;
  409. end;
  410. { export }
  411. if idtoken in [_EXPORT,_PUBLIC] then
  412. begin
  413. consume(ID);
  414. if extern_aktvarsym then
  415. Message(parser_e_not_external_and_export)
  416. else
  417. export_aktvarsym:=true;
  418. end;
  419. { external and export need a name after when no cdecl is used }
  420. if not is_cdecl then
  421. begin
  422. { dll name ? }
  423. if (extern_aktvarsym) and (token=CSTRING) then
  424. begin
  425. is_dll:=true;
  426. dll_name:=pattern;
  427. consume(CSTRING);
  428. end;
  429. consume(_NAME);
  430. C_name:=pattern;
  431. { allow also char }
  432. if token=CCHAR then
  433. consume(CCHAR)
  434. else
  435. consume(CSTRING);
  436. end;
  437. { consume the ; when export or external is used }
  438. if extern_aktvarsym or export_aktvarsym then
  439. consume(SEMICOLON);
  440. { insert in the symtable }
  441. storetokenpos:=tokenpos;
  442. tokenpos:=declarepos;
  443. if is_dll then
  444. aktvarsym:=new(pvarsym,init_dll(s,p))
  445. else
  446. aktvarsym:=new(pvarsym,init_C(s,C_name,p));
  447. tokenpos:=storetokenpos;
  448. { set some vars options }
  449. if export_aktvarsym then
  450. inc(aktvarsym^.refs);
  451. if extern_aktvarsym then
  452. aktvarsym^.var_options:=aktvarsym^.var_options or vo_is_external;
  453. { insert in the stack/datasegment }
  454. symtablestack^.insert(aktvarsym);
  455. { now we can insert it in the import lib if its a dll, or
  456. add it to the externals }
  457. if extern_aktvarsym then
  458. begin
  459. if is_dll then
  460. begin
  461. if not(current_module^.uses_imports) then
  462. begin
  463. current_module^.uses_imports:=true;
  464. importlib^.preparelib(current_module^.modulename^);
  465. end;
  466. importlib^.importvariable(aktvarsym^.mangledname,dll_name,C_name)
  467. end
  468. else
  469. externals^.concat(new(pai_external,init(aktvarsym^.mangledname,EXT_NEAR)));
  470. end;
  471. symdone:=true;
  472. end
  473. else
  474. if (is_object) and (cs_static_keyword in aktglobalswitches) and (idtoken=_STATIC) then
  475. begin
  476. current_object_option:=current_object_option or sp_static;
  477. insert_syms(symtablestack,sc,p);
  478. current_object_option:=current_object_option - sp_static;
  479. consume(_STATIC);
  480. consume(SEMICOLON);
  481. symdone:=true;
  482. end;
  483. end;
  484. { insert it in the symtable, if not done yet }
  485. if not symdone then
  486. begin
  487. if (current_object_option=sp_published) and
  488. (not((p^.deftype=objectdef) and (pobjectdef(p)^.isclass))) then
  489. Message(parser_e_cant_publish_that);
  490. insert_syms(symtablestack,sc,p);
  491. end;
  492. end;
  493. { Check for Case }
  494. if is_record and (token=_CASE) then
  495. begin
  496. maxsize:=0;
  497. consume(_CASE);
  498. s:=pattern;
  499. getsym(s,false);
  500. { may be only a type: }
  501. if assigned(srsym) and (srsym^.typ in [typesym,unitsym]) then
  502. casedef:=read_type('')
  503. else
  504. begin
  505. consume(ID);
  506. consume(COLON);
  507. casedef:=read_type('');
  508. symtablestack^.insert(new(pvarsym,init(s,casedef)));
  509. end;
  510. if not is_ordinal(casedef) then
  511. Message(type_e_ordinal_expr_expected);
  512. consume(_OF);
  513. startvarrec:=symtablestack^.datasize;
  514. repeat
  515. repeat
  516. pt:=comp_expr(true);
  517. do_firstpass(pt);
  518. if not(pt^.treetype=ordconstn) then
  519. Message(cg_e_illegal_expression);
  520. disposetree(pt);
  521. if token=COMMA then
  522. consume(COMMA)
  523. else
  524. break;
  525. until false;
  526. consume(COLON);
  527. { read the vars }
  528. consume(LKLAMMER);
  529. if token<>RKLAMMER then
  530. read_var_decs(true,false);
  531. consume(RKLAMMER);
  532. { calculates maximal variant size }
  533. maxsize:=max(maxsize,symtablestack^.datasize);
  534. { the items of the next variant are overlayed }
  535. symtablestack^.datasize:=startvarrec;
  536. if (token<>_END) and (token<>RKLAMMER) then
  537. consume(SEMICOLON)
  538. else
  539. break;
  540. until (token=_END) or (token=RKLAMMER);
  541. { at last set the record size to that of the biggest variant }
  542. symtablestack^.datasize:=maxsize;
  543. end;
  544. block_type:=old_block_type;
  545. end;
  546. function stringtype : pdef;
  547. { reads a string type with optional length }
  548. { and returns a pointer to the string }
  549. { definition }
  550. var
  551. p : ptree;
  552. d : pdef;
  553. begin
  554. consume(_STRING);
  555. if token=LECKKLAMMER then
  556. begin
  557. consume(LECKKLAMMER);
  558. p:=comp_expr(true);
  559. do_firstpass(p);
  560. if not is_constintnode(p) then
  561. Message(cg_e_illegal_expression);
  562. if (p^.value<=0) then
  563. begin
  564. Message(parser_e_invalid_string_size);
  565. p^.value:=255;
  566. end;
  567. consume(RECKKLAMMER);
  568. if p^.value>255 then
  569. d:=new(pstringdef,longinit(p^.value))
  570. else
  571. if p^.value<>255 then
  572. d:=new(pstringdef,shortinit(p^.value))
  573. else
  574. d:=cshortstringdef;
  575. disposetree(p);
  576. end
  577. else
  578. begin
  579. if cs_ansistrings in aktlocalswitches then
  580. d:=cansistringdef
  581. else
  582. d:=cshortstringdef;
  583. end;
  584. stringtype:=d;
  585. end;
  586. function id_type(var s : string) : pdef;
  587. { reads a type definition and returns a pointer }
  588. { to a appropriating pdef, s gets the name of }
  589. { the type to allow name mangling }
  590. begin
  591. s:=pattern;
  592. consume(ID);
  593. { classes can be used also in classes }
  594. if (curobjectname=pattern) and aktobjectdef^.isclass then
  595. begin
  596. id_type:=aktobjectdef;
  597. exit;
  598. end;
  599. { objects can be parameters }
  600. if (testcurobject=2) and (curobjectname=pattern) then
  601. begin
  602. id_type:=aktobjectdef;
  603. exit;
  604. end;
  605. getsym(s,true);
  606. if assigned(srsym) then
  607. begin
  608. if srsym^.typ=unitsym then
  609. begin
  610. consume(POINT);
  611. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  612. s:=pattern;
  613. consume(ID);
  614. end;
  615. if srsym^.typ<>typesym then
  616. begin
  617. Message(type_e_type_id_expected);
  618. lasttypesym:=ptypesym(srsym);
  619. id_type:=generrordef;
  620. exit;
  621. end;
  622. if not forwardsallowed then
  623. testforward_type(srsym);
  624. end;
  625. lasttypesym:=ptypesym(srsym);
  626. id_type:=ptypesym(srsym)^.definition;
  627. end;
  628. function single_type(var s : string) : pdef;
  629. { reads a string, file type or a type id and returns a name and }
  630. { pdef }
  631. var
  632. hs : string;
  633. begin
  634. case token of
  635. _STRING:
  636. begin
  637. single_type:=stringtype;
  638. s:='STRING';
  639. lasttypesym:=nil;
  640. end;
  641. _FILE:
  642. begin
  643. consume(_FILE);
  644. if token=_OF then
  645. begin
  646. consume(_OF);
  647. single_type:=new(pfiledef,init(ft_typed,single_type(hs)));
  648. s:='FILE$OF$'+hs;
  649. end
  650. else
  651. begin
  652. { single_type:=new(pfiledef,init(ft_untyped,nil));}
  653. single_type:=cfiledef;
  654. s:='FILE';
  655. end;
  656. lasttypesym:=nil;
  657. end;
  658. else single_type:=id_type(s);
  659. end;
  660. end;
  661. function object_dec(const n : stringid;fd : pobjectdef) : pdef;
  662. { this function parses an object or class declaration }
  663. var
  664. actmembertype : symprop;
  665. there_is_a_destructor : boolean;
  666. is_a_class : boolean;
  667. childof : pobjectdef;
  668. aktclass : pobjectdef;
  669. procedure constructor_head;
  670. begin
  671. consume(_CONSTRUCTOR);
  672. { must be at same level as in implementation }
  673. inc(lexlevel);
  674. parse_proc_head(poconstructor);
  675. dec(lexlevel);
  676. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'INIT') then
  677. Message(parser_e_constructorname_must_be_init);
  678. aktclass^.options:=aktclass^.options or oo_hasconstructor;
  679. consume(SEMICOLON);
  680. begin
  681. if (aktclass^.options and oo_is_class)<>0 then
  682. begin
  683. { CLASS constructors return the created instance }
  684. aktprocsym^.definition^.retdef:=aktclass;
  685. end
  686. else
  687. begin
  688. { OBJECT constructors return a boolean }
  689. {$IfDef GDB}
  690. {GDB doesn't like unnamed types !}
  691. aktprocsym^.definition^.retdef:=
  692. globaldef('boolean');
  693. {$Else GDB}
  694. aktprocsym^.definition^.retdef:=
  695. new(porddef,init(bool8bit,0,1));
  696. {$Endif GDB}
  697. end;
  698. end;
  699. end;
  700. procedure property_dec;
  701. var
  702. sym : psym;
  703. propertyparas : pdefcoll;
  704. { returns the matching procedure to access a property }
  705. function get_procdef : pprocdef;
  706. var
  707. p : pprocdef;
  708. begin
  709. p:=pprocsym(sym)^.definition;
  710. get_procdef:=nil;
  711. while assigned(p) do
  712. begin
  713. if equal_paras(p^.para1,propertyparas,true) then
  714. break;
  715. p:=p^.nextoverloaded;
  716. end;
  717. get_procdef:=p;
  718. end;
  719. var
  720. hp2,datacoll : pdefcoll;
  721. p,p2 : ppropertysym;
  722. overriden : psym;
  723. hs : string;
  724. code : word;
  725. varspez : tvarspez;
  726. sc : pstringcontainer;
  727. hp : pdef;
  728. s : string;
  729. declarepos : tfileposinfo;
  730. pp : pprocdef;
  731. pt : ptree;
  732. propname : stringid;
  733. begin
  734. { check for a class }
  735. if (aktclass^.options and oo_is_class=0) then
  736. Message(parser_e_syntax_error);
  737. consume(_PROPERTY);
  738. propertyparas:=nil;
  739. datacoll:=nil;
  740. if token=ID then
  741. begin
  742. p:=new(ppropertysym,init(pattern));
  743. propname:=pattern;
  744. consume(ID);
  745. { property parameters ? }
  746. if token=LECKKLAMMER then
  747. begin
  748. if current_object_option=sp_published then
  749. Message(parser_e_cant_publish_that_property);
  750. { create a list of the parameters in propertyparas }
  751. consume(LECKKLAMMER);
  752. inc(testcurobject);
  753. repeat
  754. if token=_VAR then
  755. begin
  756. consume(_VAR);
  757. varspez:=vs_var;
  758. end
  759. else if token=_CONST then
  760. begin
  761. consume(_CONST);
  762. varspez:=vs_const;
  763. end
  764. else varspez:=vs_value;
  765. sc:=idlist;
  766. if token=COLON then
  767. begin
  768. consume(COLON);
  769. if token=_ARRAY then
  770. begin
  771. {
  772. if (varspez<>vs_const) and
  773. (varspez<>vs_var) then
  774. begin
  775. varspez:=vs_const;
  776. Message(parser_e_illegal_open_parameter);
  777. end;
  778. }
  779. consume(_ARRAY);
  780. consume(_OF);
  781. { define range and type of range }
  782. hp:=new(parraydef,init(0,-1,s32bitdef));
  783. { define field type }
  784. parraydef(hp)^.definition:=single_type(s);
  785. end
  786. else
  787. hp:=single_type(s);
  788. end
  789. else
  790. hp:=new(pformaldef,init);
  791. s:=sc^.get_with_tokeninfo(declarepos);
  792. while s<>'' do
  793. begin
  794. new(hp2);
  795. hp2^.paratyp:=varspez;
  796. hp2^.data:=hp;
  797. hp2^.next:=propertyparas;
  798. propertyparas:=hp2;
  799. s:=sc^.get_with_tokeninfo(declarepos);
  800. end;
  801. dispose(sc,done);
  802. if token=SEMICOLON then consume(SEMICOLON)
  803. else break;
  804. until false;
  805. dec(testcurobject);
  806. consume(RECKKLAMMER);
  807. end;
  808. { overriden property ? }
  809. { force property interface, if there is a property parameter }
  810. if (token=COLON) or assigned(propertyparas) then
  811. begin
  812. consume(COLON);
  813. p^.proptype:=single_type(hs);
  814. if (idtoken=_INDEX) then
  815. begin
  816. consume(_INDEX);
  817. p^.options:=p^.options or ppo_indexed;
  818. if token=INTCONST then
  819. val(pattern,p^.index,code);
  820. consume(INTCONST);
  821. { concat a longint to the para template }
  822. new(hp2);
  823. hp2^.paratyp:=vs_value;
  824. hp2^.data:=s32bitdef;
  825. hp2^.next:=propertyparas;
  826. propertyparas:=hp2;
  827. end;
  828. end
  829. else
  830. begin
  831. { do an property override }
  832. overriden:=search_class_member(aktclass,propname);
  833. if assigned(overriden) and (overriden^.typ=propertysym) then
  834. begin
  835. { take the whole info: }
  836. p^.options:=ppropertysym(overriden)^.options;
  837. p^.index:=ppropertysym(overriden)^.index;
  838. p^.proptype:=ppropertysym(overriden)^.proptype;
  839. p^.writeaccesssym:=ppropertysym(overriden)^.writeaccesssym;
  840. p^.readaccesssym:=ppropertysym(overriden)^.readaccesssym;
  841. p^.writeaccessdef:=ppropertysym(overriden)^.writeaccessdef;
  842. p^.readaccessdef:=ppropertysym(overriden)^.readaccessdef;
  843. p^.storedsym:=ppropertysym(overriden)^.storedsym;
  844. p^.storeddef:=ppropertysym(overriden)^.storeddef;
  845. p^.default:=ppropertysym(overriden)^.default;
  846. end
  847. else
  848. begin
  849. p^.proptype:=generrordef;
  850. message(parser_e_no_property_found_to_override);
  851. end;
  852. end;
  853. if (current_object_option=sp_published) and
  854. not(p^.proptype^.is_publishable) then
  855. Message(parser_e_cant_publish_that_property);
  856. { create data defcoll to allow correct parameter checks }
  857. new(datacoll);
  858. datacoll^.paratyp:=vs_value;
  859. datacoll^.data:=p^.proptype;
  860. datacoll^.next:=nil;
  861. if (idtoken=_READ) then
  862. begin
  863. consume(_READ);
  864. sym:=search_class_member(aktclass,pattern);
  865. if not(assigned(sym)) then
  866. Message1(sym_e_unknown_id,pattern)
  867. else
  868. begin
  869. { varsym aren't allowed for an indexed property
  870. or an property with parameters }
  871. if ((sym^.typ=varsym) and
  872. { not necessary, an index forces propertyparas
  873. to be assigned
  874. }
  875. { (((p^.options and ppo_indexed)<>0) or }
  876. assigned(propertyparas)) or
  877. not(sym^.typ in [varsym,procsym]) then
  878. Message(parser_e_ill_property_access_sym);
  879. { search the matching definition }
  880. if sym^.typ=procsym then
  881. begin
  882. pp:=get_procdef;
  883. if not(assigned(pp)) or
  884. not(is_equal(pp^.retdef,p^.proptype)) then
  885. Message(parser_e_ill_property_access_sym);
  886. p^.readaccessdef:=pp;
  887. end
  888. else if sym^.typ=varsym then
  889. begin
  890. if not(is_equal(pvarsym(sym)^.definition,
  891. p^.proptype)) then
  892. Message(parser_e_ill_property_access_sym);
  893. end;
  894. p^.readaccesssym:=sym;
  895. end;
  896. consume(ID);
  897. end;
  898. if (idtoken=_WRITE) then
  899. begin
  900. consume(_WRITE);
  901. sym:=search_class_member(aktclass,pattern);
  902. if not(assigned(sym)) then
  903. Message1(sym_e_unknown_id,pattern)
  904. else
  905. begin
  906. if ((sym^.typ=varsym) and
  907. assigned(propertyparas)) or
  908. not(sym^.typ in [varsym,procsym]) then
  909. Message(parser_e_ill_property_access_sym);
  910. { search the matching definition }
  911. if sym^.typ=procsym then
  912. begin
  913. { insert data entry to check access method }
  914. datacoll^.next:=propertyparas;
  915. propertyparas:=datacoll;
  916. pp:=get_procdef;
  917. { ... and remove it }
  918. propertyparas:=propertyparas^.next;
  919. datacoll^.next:=nil;
  920. if not(assigned(pp)) then
  921. Message(parser_e_ill_property_access_sym);
  922. p^.writeaccessdef:=pp;
  923. end
  924. else if sym^.typ=varsym then
  925. begin
  926. if not(is_equal(pvarsym(sym)^.definition,
  927. p^.proptype)) then
  928. Message(parser_e_ill_property_access_sym);
  929. end;
  930. p^.writeaccesssym:=sym;
  931. end;
  932. consume(ID);
  933. end;
  934. if (idtoken=_STORED) then
  935. begin
  936. consume(_STORED);
  937. Message(parser_w_stored_not_implemented);
  938. { !!!!!!!! }
  939. end;
  940. if (idtoken=_DEFAULT) then
  941. begin
  942. consume(_DEFAULT);
  943. if not(is_ordinal(p^.proptype) or
  944. ((p^.proptype^.deftype=setdef) and
  945. (psetdef(p^.proptype)^.settype=smallset)
  946. ) or
  947. assigned(propertyparas)
  948. ) then
  949. Message(parser_e_property_cant_have_a_default_value);
  950. pt:=comp_expr(true);
  951. pt:=gentypeconvnode(pt,p^.proptype);
  952. do_firstpass(pt);
  953. if not(is_constnode(pt)) then
  954. Message(parser_e_property_default_value_must_const);
  955. if pt^.treetype=setconstn then
  956. p^.default:=plongint(pt^.value_set)^
  957. else
  958. p^.default:=pt^.value;
  959. disposetree(pt);
  960. end
  961. else if (idtoken=_NODEFAULT) then
  962. begin
  963. consume(_NODEFAULT);
  964. p^.default:=0;
  965. end;
  966. symtablestack^.insert(p);
  967. { default property ? }
  968. consume(SEMICOLON);
  969. if (idtoken=_DEFAULT) then
  970. begin
  971. consume(_DEFAULT);
  972. p2:=search_default_property(aktclass);
  973. if assigned(p2) then
  974. message1(parser_e_only_one_default_property,
  975. pobjectdef(p2^.owner^.defowner)^.name^)
  976. else
  977. begin
  978. p^.options:=p^.options or ppo_defaultproperty;
  979. if not(assigned(propertyparas)) then
  980. message(parser_e_property_need_paras);
  981. end;
  982. consume(SEMICOLON);
  983. end;
  984. { clean up }
  985. if assigned(datacoll) then
  986. disposepdefcoll(datacoll);
  987. end
  988. else
  989. begin
  990. consume(ID);
  991. consume(SEMICOLON);
  992. end;
  993. if assigned(propertyparas) then
  994. disposepdefcoll(propertyparas);
  995. end;
  996. procedure destructor_head;
  997. begin
  998. consume(_DESTRUCTOR);
  999. inc(lexlevel);
  1000. parse_proc_head(podestructor);
  1001. dec(lexlevel);
  1002. if (cs_constructor_name in aktglobalswitches) and (aktprocsym^.name<>'DONE') then
  1003. Message(parser_e_destructorname_must_be_done);
  1004. aktclass^.options:=aktclass^.options or oo_hasdestructor;
  1005. consume(SEMICOLON);
  1006. if assigned(aktprocsym^.definition^.para1) then
  1007. Message(parser_e_no_paras_for_destructor);
  1008. { no return value }
  1009. aktprocsym^.definition^.retdef:=voiddef;
  1010. end;
  1011. var
  1012. hs : string;
  1013. pcrd : pclassrefdef;
  1014. hp1 : pdef;
  1015. hfp : pforwardpointer;
  1016. oldprocsym : pprocsym;
  1017. oldparse_only : boolean;
  1018. classnamelabel : plabel;
  1019. storetypeforwardsallowed : boolean;
  1020. begin
  1021. {Nowadays aktprocsym may already have a value, so we need to save
  1022. it.}
  1023. oldprocsym:=aktprocsym;
  1024. { forward is resolved }
  1025. if assigned(fd) then
  1026. fd^.options:=fd^.options and not(oo_isforward);
  1027. there_is_a_destructor:=false;
  1028. actmembertype:=sp_public;
  1029. { objects and class types can't be declared local }
  1030. if (symtablestack^.symtabletype<>globalsymtable) and
  1031. (symtablestack^.symtabletype<>staticsymtable) then
  1032. Message(parser_e_no_local_objects);
  1033. storetypeforwardsallowed:=typecanbeforward;
  1034. if m_tp in aktmodeswitches then
  1035. typecanbeforward:=false;
  1036. { distinguish classes and objects }
  1037. if token=_OBJECT then
  1038. begin
  1039. is_a_class:=false;
  1040. consume(_OBJECT)
  1041. end
  1042. else
  1043. begin
  1044. is_a_class:=true;
  1045. consume(_CLASS);
  1046. if not(assigned(fd)) and (token=_OF) then
  1047. begin
  1048. { a hack, but it's easy to handle }
  1049. { class reference type }
  1050. consume(_OF);
  1051. if typecanbeforward then
  1052. forwardsallowed:=true;
  1053. hp1:=single_type(hs);
  1054. { accept hp1, if is a forward def ...}
  1055. if ((lasttypesym<>nil)
  1056. and ((lasttypesym^.properties and sp_forwarddef)<>0)) or
  1057. { or a class
  1058. (if the foward defined type is a class is checked, when
  1059. the forward is resolved)
  1060. }
  1061. ((hp1^.deftype=objectdef) and (
  1062. (pobjectdef(hp1)^.options and oo_is_class)<>0)) then
  1063. begin
  1064. pcrd:=new(pclassrefdef,init(hp1));
  1065. object_dec:=pcrd;
  1066. {I add big troubles here
  1067. with var p : ^byte in graph.putimage
  1068. because a save_forward was called and
  1069. no resolve forward
  1070. => so the definition was rewritten after
  1071. having been disposed !!
  1072. Strange problems appeared !!!!}
  1073. {Anyhow forwards should only be allowed
  1074. inside a type statement ??
  1075. don't you think so }
  1076. if (lasttypesym<>nil) and ((lasttypesym^.properties and sp_forwarddef)<>0) then
  1077. begin
  1078. new(hfp);
  1079. hfp^.next:=lasttypesym^.forwardpointer;
  1080. hfp^.def:=ppointerdef(pcrd);
  1081. lasttypesym^.forwardpointer:=hfp;
  1082. end;
  1083. forwardsallowed:=false;
  1084. end
  1085. else
  1086. begin
  1087. Message(type_e_class_type_expected);
  1088. object_dec:=new(perrordef,init);
  1089. end;
  1090. exit;
  1091. end
  1092. { forward class }
  1093. else if not(assigned(fd)) and (token=SEMICOLON) then
  1094. begin
  1095. { also anonym objects aren't allow (o : object a : longint; end;) }
  1096. if n='' then
  1097. begin
  1098. Message(parser_f_no_anonym_objects)
  1099. end;
  1100. if n='TOBJECT' then
  1101. begin
  1102. aktclass:=new(pobjectdef,init(n,nil));
  1103. class_tobject:=aktclass;
  1104. end
  1105. else
  1106. aktclass:=new(pobjectdef,init(n,nil));
  1107. aktclass^.options:=aktclass^.options or oo_is_class or oo_isforward;
  1108. { all classes must have a vmt !! at offset zero }
  1109. if (aktclass^.options and oo_hasvmt)=0 then
  1110. aktclass^.insertvmt;
  1111. object_dec:=aktclass;
  1112. exit;
  1113. end;
  1114. end;
  1115. { also anonym objects aren't allow (o : object a : longint; end;) }
  1116. if n='' then
  1117. Message(parser_f_no_anonym_objects);
  1118. { read the parent class }
  1119. if token=LKLAMMER then
  1120. begin
  1121. consume(LKLAMMER);
  1122. { does not allow objects.tobject !! }
  1123. {if token<>ID then
  1124. consume(ID);
  1125. getsym(pattern,true);}
  1126. childof:=pobjectdef(id_type(pattern));
  1127. if (childof^.deftype<>objectdef) then
  1128. begin
  1129. Message(type_e_class_type_expected);
  1130. childof:=nil;
  1131. end
  1132. else
  1133. begin
  1134. { a mix of class and object isn't allowed }
  1135. if (((childof^.options and oo_is_class)<>0) and not is_a_class) or
  1136. (((childof^.options and oo_is_class)=0) and is_a_class) then
  1137. Message(parser_e_mix_of_classes_and_objects);
  1138. end;
  1139. if assigned(fd) then
  1140. begin
  1141. { the forward of the child must be resolved to get
  1142. correct field addresses
  1143. }
  1144. if (childof^.options and oo_isforward)<>0 then
  1145. Message1(parser_e_forward_declaration_must_be_resolved,childof^.name^);
  1146. aktclass:=fd;
  1147. { we must inherit several options !!
  1148. this was missing !!
  1149. all is now done in set_parent
  1150. including symtable datasize setting PM }
  1151. fd^.set_parent(childof);
  1152. end
  1153. else
  1154. aktclass:=new(pobjectdef,init(n,childof));
  1155. consume(RKLAMMER);
  1156. end
  1157. { if no parent class, then a class get tobject as parent }
  1158. else if is_a_class then
  1159. begin
  1160. { is the current class tobject? }
  1161. { so you could define your own tobject }
  1162. if n='TOBJECT' then
  1163. begin
  1164. if assigned(fd) then
  1165. aktclass:=fd
  1166. else
  1167. aktclass:=new(pobjectdef,init(n,nil));
  1168. class_tobject:=aktclass;
  1169. end
  1170. else
  1171. begin
  1172. childof:=class_tobject;
  1173. if assigned(fd) then
  1174. begin
  1175. { the forward of the child must be resolved to get
  1176. correct field addresses
  1177. }
  1178. if (childof^.options and oo_isforward)<>0 then
  1179. Message1(parser_e_forward_declaration_must_be_resolved,childof^.name^);
  1180. aktclass:=fd;
  1181. aktclass^.set_parent(childof);
  1182. end
  1183. else
  1184. begin
  1185. aktclass:=new(pobjectdef,init(n,childof));
  1186. aktclass^.set_parent(childof);
  1187. end;
  1188. end;
  1189. end
  1190. else
  1191. aktclass:=new(pobjectdef,init(n,nil));
  1192. { set the class attribute }
  1193. if is_a_class then
  1194. begin
  1195. aktclass^.options:=aktclass^.options or oo_is_class;
  1196. if (cs_generate_rtti in aktlocalswitches) or
  1197. (assigned(aktclass^.childof) and
  1198. ((aktclass^.childof^.options and oo_can_have_published)<>0)
  1199. ) then
  1200. aktclass^.options:=aktclass^.options or oo_can_have_published;
  1201. end;
  1202. aktobjectdef:=aktclass;
  1203. { default access is public }
  1204. actmembertype:=sp_public;
  1205. aktclass^.publicsyms^.next:=symtablestack;
  1206. symtablestack:=aktclass^.publicsyms;
  1207. procinfo._class:=aktclass;
  1208. testcurobject:=1;
  1209. curobjectname:=n;
  1210. { short class declaration ? }
  1211. if (not is_a_class) or (token<>SEMICOLON) then
  1212. begin
  1213. { Parse componenten }
  1214. repeat
  1215. if actmembertype=sp_private then
  1216. aktclass^.options:=aktclass^.options or oo_hasprivate;
  1217. if actmembertype=sp_protected then
  1218. aktclass^.options:=aktclass^.options or oo_hasprotected;
  1219. case token of
  1220. ID : begin
  1221. case idtoken of
  1222. _PRIVATE : begin
  1223. consume(_PRIVATE);
  1224. actmembertype:=sp_private;
  1225. current_object_option:=sp_private;
  1226. end;
  1227. _PROTECTED : begin
  1228. consume(_PROTECTED);
  1229. current_object_option:=sp_protected;
  1230. actmembertype:=sp_protected;
  1231. end;
  1232. _PUBLIC : begin
  1233. consume(_PUBLIC);
  1234. current_object_option:=sp_public;
  1235. actmembertype:=sp_public;
  1236. end;
  1237. _PUBLISHED : begin
  1238. if (aktclass^.options and oo_can_have_published)=0 then
  1239. Message(parser_e_cant_have_published);
  1240. consume(_PUBLISHED);
  1241. current_object_option:=sp_published;
  1242. actmembertype:=sp_published;
  1243. end;
  1244. else
  1245. read_var_decs(false,true);
  1246. end;
  1247. end;
  1248. _PROPERTY : property_dec;
  1249. _PROCEDURE,
  1250. _FUNCTION,
  1251. _CLASS : begin
  1252. oldparse_only:=parse_only;
  1253. parse_only:=true;
  1254. parse_proc_dec;
  1255. parse_only:=oldparse_only;
  1256. case idtoken of
  1257. _DYNAMIC,
  1258. _VIRTUAL : begin
  1259. if actmembertype=sp_private then
  1260. Message(parser_w_priv_meth_not_virtual);
  1261. consume(idtoken);
  1262. consume(SEMICOLON);
  1263. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or povirtualmethod;
  1264. aktclass^.options:=aktclass^.options or oo_hasvirtual;
  1265. end;
  1266. _OVERRIDE : begin
  1267. consume(_OVERRIDE);
  1268. consume(SEMICOLON);
  1269. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or
  1270. pooverridingmethod or povirtualmethod;
  1271. end;
  1272. end;
  1273. if idtoken=_abstract then
  1274. begin
  1275. if (aktprocsym^.definition^.options and povirtualmethod)<>0 then
  1276. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poabstractmethod
  1277. else
  1278. Message(parser_e_only_virtual_methods_abstract);
  1279. consume(_ABSTRACT);
  1280. consume(SEMICOLON);
  1281. { the method is defined }
  1282. aktprocsym^.definition^.forwarddef:=false;
  1283. end;
  1284. if (cs_static_keyword in aktglobalswitches) and (idtoken=_STATIC) then
  1285. begin
  1286. consume(_STATIC);
  1287. consume(SEMICOLON);
  1288. aktprocsym^.properties:=aktprocsym^.properties or sp_static;
  1289. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or postaticmethod;
  1290. end;
  1291. end;
  1292. _CONSTRUCTOR : begin
  1293. if actmembertype<>sp_public then
  1294. Message(parser_w_constructor_should_be_public);
  1295. oldparse_only:=parse_only;
  1296. parse_only:=true;
  1297. constructor_head;
  1298. parse_only:=oldparse_only;
  1299. case idtoken of
  1300. _DYNAMIC,
  1301. _VIRTUAL : begin
  1302. if not(aktclass^.isclass) then
  1303. Message(parser_e_constructor_cannot_be_not_virtual)
  1304. else
  1305. begin
  1306. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or povirtualmethod;
  1307. aktclass^.options:=aktclass^.options or oo_hasvirtual;
  1308. end;
  1309. consume(idtoken);
  1310. consume(SEMICOLON);
  1311. end;
  1312. _OVERRIDE : begin
  1313. if (aktclass^.options and oo_is_class=0) then
  1314. Message(parser_e_constructor_cannot_be_not_virtual)
  1315. else
  1316. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or
  1317. pooverridingmethod or povirtualmethod;
  1318. consume(_OVERRIDE);
  1319. consume(SEMICOLON);
  1320. end;
  1321. end;
  1322. end;
  1323. _DESTRUCTOR : begin
  1324. if there_is_a_destructor then
  1325. Message(parser_n_only_one_destructor);
  1326. there_is_a_destructor:=true;
  1327. if actmembertype<>sp_public then
  1328. Message(parser_w_destructor_should_be_public);
  1329. oldparse_only:=parse_only;
  1330. parse_only:=true;
  1331. destructor_head;
  1332. parse_only:=oldparse_only;
  1333. case idtoken of
  1334. _DYNAMIC,
  1335. _VIRTUAL : begin
  1336. consume(idtoken);
  1337. consume(SEMICOLON);
  1338. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or povirtualmethod;
  1339. aktclass^.options:=aktclass^.options or oo_hasvirtual;
  1340. end;
  1341. _OVERRIDE : begin
  1342. consume(_OVERRIDE);
  1343. consume(SEMICOLON);
  1344. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or
  1345. pooverridingmethod or povirtualmethod;
  1346. end;
  1347. end;
  1348. end;
  1349. _END : begin
  1350. consume(_END);
  1351. break;
  1352. end;
  1353. else
  1354. consume(ID); { Give a ident expected message, like tp7 }
  1355. end;
  1356. until false;
  1357. current_object_option:=sp_public;
  1358. end;
  1359. testcurobject:=0;
  1360. curobjectname:='';
  1361. typecanbeforward:=storetypeforwardsallowed;
  1362. { generate vmt space if needed }
  1363. if ((aktclass^.options and
  1364. (oo_hasvirtual or oo_hasconstructor or
  1365. oo_hasdestructor or oo_is_class))<>0) and
  1366. ((aktclass^.options and
  1367. oo_hasvmt)=0) then
  1368. aktclass^.insertvmt;
  1369. if (cs_smartlink in aktmoduleswitches) then
  1370. datasegment^.concat(new(pai_cut,init));
  1371. { write extended info for classes }
  1372. if is_a_class then
  1373. begin
  1374. if (aktclass^.options and oo_can_have_published)<>0 then
  1375. aktclass^.generate_rtti;
  1376. { write class name }
  1377. getlabel(classnamelabel);
  1378. datasegment^.concat(new(pai_label,init(classnamelabel)));
  1379. datasegment^.concat(new(pai_const,init_8bit(length(aktclass^.name^))));
  1380. datasegment^.concat(new(pai_string,init(aktclass^.name^)));
  1381. { interface table }
  1382. datasegment^.concat(new(pai_const,init_32bit(0)));
  1383. { auto table }
  1384. datasegment^.concat(new(pai_const,init_32bit(0)));
  1385. { inittable for con-/destruction }
  1386. if aktclass^.needs_inittable then
  1387. datasegment^.concat(new(pai_const,init_symbol(strpnew(lab2str(aktclass^.get_inittable_label)))))
  1388. else
  1389. datasegment^.concat(new(pai_const,init_32bit(0)));
  1390. { pointer to type info of published section }
  1391. if (aktclass^.options and oo_can_have_published)<>0 then
  1392. datasegment^.concat(new(pai_const,init_symbol(strpnew(aktclass^.rtti_name))))
  1393. else
  1394. datasegment^.concat(new(pai_const,init_32bit(0)));
  1395. { pointer to field table }
  1396. datasegment^.concat(new(pai_const,init_32bit(0)));
  1397. { pointer to method table }
  1398. datasegment^.concat(new(pai_const,init_32bit(0)));
  1399. { pointer to dynamic table }
  1400. datasegment^.concat(new(pai_const,init_32bit(0)));
  1401. { pointer to class name string }
  1402. datasegment^.concat(new(pai_const,init_symbol(strpnew(lab2str(classnamelabel)))));
  1403. end;
  1404. {$ifdef GDB}
  1405. { generate the VMT }
  1406. if (cs_debuginfo in aktmoduleswitches) and
  1407. ((aktclass^.options and oo_hasvmt)<>0) then
  1408. begin
  1409. do_count_dbx:=true;
  1410. if assigned(aktclass^.owner) and assigned(aktclass^.owner^.name) then
  1411. datasegment^.concat(new(pai_stabs,init(strpnew('"vmt_'+aktclass^.owner^.name^+n+':S'+
  1412. typeglobalnumber('__vtbl_ptr_type')+'",'+tostr(N_STSYM)+',0,0,'+aktclass^.vmt_mangledname))));
  1413. end;
  1414. {$endif GDB}
  1415. if ((aktclass^.options and oo_hasvmt)<>0) then
  1416. begin
  1417. datasegment^.concat(new(pai_symbol,init_global(aktclass^.vmt_mangledname)));
  1418. { determine the size with publicsyms^.datasize, because }
  1419. { size gives back 4 for classes }
  1420. datasegment^.concat(new(pai_const,init_32bit(aktclass^.publicsyms^.datasize)));
  1421. datasegment^.concat(new(pai_const,init_32bit(-aktclass^.publicsyms^.datasize)));
  1422. { write pointer to parent VMT, this isn't implemented in TP }
  1423. { but this is not used in FPC ? (PM) }
  1424. { it's not used yet, but the delphi-operators as and is need it (FK) }
  1425. { it is not written for parents that don't have any vmt !! }
  1426. if assigned(aktclass^.childof) and
  1427. ((aktclass^.childof^.options and oo_hasvmt)<>0) then
  1428. begin
  1429. datasegment^.concat(new(pai_const,init_symbol(strpnew(aktclass^.childof^.vmt_mangledname))));
  1430. if aktclass^.childof^.owner^.symtabletype=unitsymtable then
  1431. concat_external(aktclass^.childof^.vmt_mangledname,EXT_NEAR);
  1432. end
  1433. else
  1434. datasegment^.concat(new(pai_const,init_32bit(0)));
  1435. { this generates the entries }
  1436. genvmt(aktclass);
  1437. end;
  1438. { restore old state }
  1439. symtablestack:=symtablestack^.next;
  1440. procinfo._class:=nil;
  1441. aktobjectdef:=nil;
  1442. {Restore the aktprocsym.}
  1443. aktprocsym:=oldprocsym;
  1444. object_dec:=aktclass;
  1445. end;
  1446. { reads a record declaration }
  1447. function record_dec : pdef;
  1448. var
  1449. symtable : psymtable;
  1450. storetypeforwardsallowed : boolean;
  1451. begin
  1452. symtable:=new(psymtable,init(recordsymtable));
  1453. symtable^.next:=symtablestack;
  1454. symtablestack:=symtable;
  1455. consume(_RECORD);
  1456. storetypeforwardsallowed:=typecanbeforward;
  1457. if m_tp in aktmodeswitches then
  1458. typecanbeforward:=false;
  1459. read_var_decs(true,false);
  1460. { may be scale record size to a size of n*4 ? }
  1461. if ((symtablestack^.datasize mod aktpackrecords)<>0) then
  1462. inc(symtablestack^.datasize,aktpackrecords-(symtablestack^.datasize mod aktpackrecords));
  1463. consume(_END);
  1464. typecanbeforward:=storetypeforwardsallowed;
  1465. symtablestack:=symtable^.next;
  1466. record_dec:=new(precdef,init(symtable));
  1467. end;
  1468. { reads a type definition and returns a pointer to it }
  1469. function read_type(const name : stringid) : pdef;
  1470. function handle_procvar:Pprocvardef;
  1471. var
  1472. sc : pstringcontainer;
  1473. s : string;
  1474. p : pdef;
  1475. varspez : tvarspez;
  1476. procvardef : pprocvardef;
  1477. begin
  1478. procvardef:=new(pprocvardef,init);
  1479. if token=LKLAMMER then
  1480. begin
  1481. consume(LKLAMMER);
  1482. inc(testcurobject);
  1483. repeat
  1484. case token of
  1485. _VAR :
  1486. begin
  1487. consume(_VAR);
  1488. varspez:=vs_var;
  1489. end;
  1490. _CONST :
  1491. begin
  1492. consume(_CONST);
  1493. varspez:=vs_const;
  1494. end;
  1495. else
  1496. varspez:=vs_value;
  1497. end;
  1498. sc:=idlist;
  1499. if (token=COLON) or (varspez=vs_value) then
  1500. begin
  1501. consume(COLON);
  1502. if token=_ARRAY then
  1503. begin
  1504. consume(_ARRAY);
  1505. consume(_OF);
  1506. { define range and type of range }
  1507. p:=new(Parraydef,init(0,-1,s32bitdef));
  1508. { array of const ? }
  1509. if (token=_CONST) and (m_objpas in aktmodeswitches) then
  1510. begin
  1511. consume(_CONST);
  1512. srsym:=nil;
  1513. if assigned(objpasunit) then
  1514. getsymonlyin(objpasunit,'TVARREC');
  1515. if not assigned(srsym) then
  1516. InternalError(1234124);
  1517. Parraydef(p)^.definition:=ptypesym(srsym)^.definition;
  1518. Parraydef(p)^.IsArrayOfConst:=true;
  1519. end
  1520. else
  1521. begin
  1522. { define field type }
  1523. Parraydef(p)^.definition:=single_type(s);
  1524. end;
  1525. end
  1526. else
  1527. p:=single_type(s);
  1528. end
  1529. else
  1530. p:=new(pformaldef,init);
  1531. while not sc^.empty do
  1532. begin
  1533. s:=sc^.get;
  1534. procvardef^.concatdef(p,varspez);
  1535. end;
  1536. dispose(sc,done);
  1537. if token=SEMICOLON then
  1538. consume(SEMICOLON)
  1539. else
  1540. break;
  1541. until false;
  1542. dec(testcurobject);
  1543. consume(RKLAMMER);
  1544. end;
  1545. handle_procvar:=procvardef;
  1546. end;
  1547. var
  1548. hp1,p : pdef;
  1549. aufdef : penumdef;
  1550. aufsym : penumsym;
  1551. ap : parraydef;
  1552. s : stringid;
  1553. l,v,oldaktpackrecords : longint;
  1554. hs : string;
  1555. procedure expr_type;
  1556. var
  1557. pt1,pt2 : ptree;
  1558. begin
  1559. { use of current parsed object ? }
  1560. if (token=ID) and (testcurobject=2) and (curobjectname=pattern) then
  1561. begin
  1562. consume(ID);
  1563. p:=aktobjectdef;
  1564. exit;
  1565. end;
  1566. { we can't accept a equal in type }
  1567. pt1:=comp_expr(not(ignore_equal));
  1568. do_firstpass(pt1);
  1569. if (token=POINTPOINT) then
  1570. begin
  1571. consume(POINTPOINT);
  1572. { get high value of range }
  1573. pt2:=comp_expr(not(ignore_equal));
  1574. do_firstpass(pt2);
  1575. { both must be evaluated to constants now }
  1576. if (pt1^.treetype<>ordconstn) or (pt2^.treetype<>ordconstn) then
  1577. Message(sym_e_error_in_type_def)
  1578. else
  1579. begin
  1580. { check types }
  1581. if not is_equal(pt1^.resulttype,pt2^.resulttype) then
  1582. Message(type_e_mismatch)
  1583. else
  1584. begin
  1585. { Check bounds }
  1586. if pt2^.value<pt1^.value then
  1587. Message(cg_e_upper_lower_than_lower)
  1588. else
  1589. begin
  1590. { All checks passed, create the new def }
  1591. case pt1^.resulttype^.deftype of
  1592. enumdef : p:=new(penumdef,init_subrange(penumdef(pt1^.resulttype),pt1^.value,pt2^.value));
  1593. orddef : begin
  1594. if is_char(pt1^.resulttype) then
  1595. p:=new(porddef,init(uchar,pt1^.value,pt2^.value))
  1596. else
  1597. if is_boolean(pt1^.resulttype) then
  1598. p:=new(porddef,init(bool8bit,pt1^.value,pt2^.value))
  1599. else
  1600. p:=new(porddef,init(uauto,pt1^.value,pt2^.value));
  1601. end;
  1602. end;
  1603. end;
  1604. end;
  1605. end;
  1606. disposetree(pt2);
  1607. end
  1608. else
  1609. begin
  1610. { a simple type renaming }
  1611. if (pt1^.treetype=typen) then
  1612. p:=pt1^.resulttype
  1613. else
  1614. Message(sym_e_error_in_type_def);
  1615. end;
  1616. disposetree(pt1);
  1617. end;
  1618. var
  1619. pt : ptree;
  1620. procedure array_dec;
  1621. var
  1622. lowval,
  1623. highval : longint;
  1624. arraytype : pdef;
  1625. begin
  1626. consume(_ARRAY);
  1627. consume(LECKKLAMMER);
  1628. { defaults }
  1629. arraytype:=generrordef;
  1630. lowval:=$80000000;
  1631. highval:=$7fffffff;
  1632. p:=nil;
  1633. repeat
  1634. { read the expression and check it }
  1635. pt:=expr;
  1636. if pt^.treetype=typen then
  1637. begin
  1638. case pt^.resulttype^.deftype of
  1639. enumdef : begin
  1640. lowval:=penumdef(pt^.resulttype)^.min;
  1641. highval:=penumdef(pt^.resulttype)^.max;
  1642. arraytype:=pt^.resulttype;
  1643. end;
  1644. orddef : begin
  1645. case porddef(pt^.resulttype)^.typ of
  1646. s8bit,u8bit,
  1647. s16bit,u16bit,
  1648. s32bit : begin
  1649. lowval:=porddef(pt^.resulttype)^.low;
  1650. highval:=porddef(pt^.resulttype)^.high;
  1651. arraytype:=pt^.resulttype;
  1652. end;
  1653. bool8bit,
  1654. bool16bit,
  1655. bool32bit : begin
  1656. lowval:=0;
  1657. highval:=1;
  1658. arraytype:=pt^.resulttype;
  1659. end;
  1660. uchar : begin
  1661. lowval:=0;
  1662. highval:=255;
  1663. arraytype:=pt^.resulttype;
  1664. end;
  1665. else
  1666. Message(sym_e_error_in_type_def);
  1667. end;
  1668. end;
  1669. else
  1670. Message(sym_e_error_in_type_def);
  1671. end
  1672. end
  1673. else
  1674. begin
  1675. do_firstpass(pt);
  1676. if (pt^.treetype=rangen) then
  1677. begin
  1678. if (pt^.left^.treetype=ordconstn) and
  1679. (pt^.right^.treetype=ordconstn) then
  1680. begin
  1681. lowval:=pt^.left^.value;
  1682. highval:=pt^.right^.value;
  1683. if highval<lowval then
  1684. begin
  1685. Message(parser_e_array_lower_less_than_upper_bound);
  1686. highval:=lowval;
  1687. end;
  1688. arraytype:=pt^.right^.resulttype;
  1689. end
  1690. else
  1691. Message(type_e_cant_eval_constant_expr);
  1692. end
  1693. else
  1694. Message(sym_e_error_in_type_def)
  1695. end;
  1696. disposetree(pt);
  1697. { create arraydef }
  1698. if p=nil then
  1699. begin
  1700. ap:=new(parraydef,init(lowval,highval,arraytype));
  1701. p:=ap;
  1702. end
  1703. else
  1704. begin
  1705. ap^.definition:=new(parraydef,init(lowval,highval,arraytype));
  1706. ap:=parraydef(ap^.definition);
  1707. end;
  1708. if token=COMMA then
  1709. consume(COMMA)
  1710. else
  1711. break;
  1712. until false;
  1713. consume(RECKKLAMMER);
  1714. consume(_OF);
  1715. hp1:=read_type('');
  1716. { if no error, set element type }
  1717. if assigned(ap) then
  1718. ap^.definition:=hp1;
  1719. end;
  1720. var
  1721. hfp : pforwardpointer;
  1722. begin
  1723. p:=nil;
  1724. case token of
  1725. _STRING,_FILE:
  1726. p:=single_type(hs);
  1727. LKLAMMER:
  1728. begin
  1729. consume(LKLAMMER);
  1730. { allow negativ value_str }
  1731. l:=-1;
  1732. aufsym := Nil;
  1733. aufdef:=new(penumdef,init);
  1734. repeat
  1735. s:=pattern;
  1736. consume(ID);
  1737. if token=ASSIGNMENT then
  1738. begin
  1739. consume(ASSIGNMENT);
  1740. v:=get_intconst;
  1741. { please leave that a note, allows type save }
  1742. { declarations in the win32 units ! }
  1743. if v<=l then
  1744. Message(parser_n_duplicate_enum);
  1745. l:=v;
  1746. end
  1747. else
  1748. inc(l);
  1749. constsymtable^.insert(new(penumsym,init(s,aufdef,l)));
  1750. if token=COMMA then
  1751. consume(COMMA)
  1752. else
  1753. break;
  1754. until false;
  1755. {aufdef^.max:=l;
  1756. if we allow unordered enums
  1757. this can be wrong
  1758. min and max are now set in tenumsym.init PM }
  1759. p:=aufdef;
  1760. consume(RKLAMMER);
  1761. end;
  1762. _ARRAY:
  1763. array_dec;
  1764. _SET:
  1765. begin
  1766. consume(_SET);
  1767. consume(_OF);
  1768. hp1:=read_type('');
  1769. if assigned(hp1) then
  1770. begin
  1771. case hp1^.deftype of
  1772. { don't forget that min can be negativ PM }
  1773. enumdef : if penumdef(hp1)^.min>=0 then
  1774. p:=new(psetdef,init(hp1,penumdef(hp1)^.max))
  1775. else
  1776. Message(sym_e_ill_type_decl_set);
  1777. orddef : begin
  1778. case porddef(hp1)^.typ of
  1779. uchar : p:=new(psetdef,init(hp1,255));
  1780. u8bit,s8bit,u16bit,s16bit,s32bit :
  1781. begin
  1782. if (porddef(hp1)^.low>=0) then
  1783. p:=new(psetdef,init(hp1,porddef(hp1)^.high))
  1784. else Message(sym_e_ill_type_decl_set);
  1785. end;
  1786. else Message(sym_e_ill_type_decl_set);
  1787. end;
  1788. end;
  1789. else Message(sym_e_ill_type_decl_set);
  1790. end;
  1791. end
  1792. else
  1793. p:=generrordef;
  1794. end;
  1795. CARET:
  1796. begin
  1797. consume(CARET);
  1798. { forwards allowed only inside TYPE statements }
  1799. if typecanbeforward then
  1800. forwardsallowed:=true;
  1801. hp1:=single_type(hs);
  1802. p:=new(ppointerdef,init(hp1));
  1803. {I add big troubles here
  1804. with var p : ^byte in graph.putimage
  1805. because a save_forward was called and
  1806. no resolve forward
  1807. => so the definition was rewritten after
  1808. having been disposed !!
  1809. Strange problems appeared !!!!}
  1810. {Anyhow forwards should only be allowed
  1811. inside a type statement ??
  1812. don't you think so }
  1813. if (lasttypesym<>nil) and ((lasttypesym^.properties and sp_forwarddef)<>0) then
  1814. begin
  1815. new(hfp);
  1816. hfp^.next:=lasttypesym^.forwardpointer;
  1817. hfp^.def:=ppointerdef(p);
  1818. lasttypesym^.forwardpointer:=hfp;
  1819. end;
  1820. forwardsallowed:=false;
  1821. end;
  1822. _RECORD:
  1823. p:=record_dec;
  1824. _PACKED:
  1825. begin
  1826. consume(_PACKED);
  1827. if token=_ARRAY then
  1828. array_dec
  1829. else
  1830. begin
  1831. oldaktpackrecords:=aktpackrecords;
  1832. aktpackrecords:=1;
  1833. if token in [_CLASS,_OBJECT] then
  1834. p:=object_dec(name,nil)
  1835. else
  1836. p:=record_dec;
  1837. aktpackrecords:=oldaktpackrecords;
  1838. end;
  1839. end;
  1840. _CLASS,
  1841. _OBJECT:
  1842. p:=object_dec(name,nil);
  1843. _PROCEDURE:
  1844. begin
  1845. consume(_PROCEDURE);
  1846. p:=handle_procvar;
  1847. if token=_OF then
  1848. begin
  1849. consume(_OF);
  1850. consume(_OBJECT);
  1851. pprocvardef(p)^.options:=pprocvardef(p)^.options or pomethodpointer;
  1852. end;
  1853. end;
  1854. _FUNCTION:
  1855. begin
  1856. consume(_FUNCTION);
  1857. p:=handle_procvar;
  1858. consume(COLON);
  1859. pprocvardef(p)^.retdef:=single_type(hs);
  1860. if token=_OF then
  1861. begin
  1862. consume(_OF);
  1863. consume(_OBJECT);
  1864. pprocvardef(p)^.options:=pprocvardef(p)^.options or pomethodpointer;
  1865. end;
  1866. end;
  1867. else
  1868. expr_type;
  1869. end;
  1870. if p=nil then
  1871. p:=generrordef;
  1872. read_type:=p;
  1873. end;
  1874. { reads a type declaration to the symbol table }
  1875. procedure type_dec;
  1876. var
  1877. typename : stringid;
  1878. newtype : ptypesym;
  1879. {$ifdef dummy}
  1880. olddef,newdef : pdef;
  1881. s : string;
  1882. {$endif dummy}
  1883. begin
  1884. block_type:=bt_type;
  1885. consume(_TYPE);
  1886. typecanbeforward:=true;
  1887. repeat
  1888. typename:=pattern;
  1889. consume(ID);
  1890. consume(EQUAL);
  1891. { here you loose the strictness of pascal
  1892. for which a redefinition like
  1893. childtype = parenttype;
  1894. child2type = parenttype;
  1895. does not make the two child types equal !!
  1896. here all vars from childtype and child2type
  1897. get the definition of parenttype !! }
  1898. {$ifdef testequaltype}
  1899. if (token = ID) or (token=_FILE) or (token=_STRING) then
  1900. begin
  1901. olddef := single_type(s);
  1902. { make a clone of olddef }
  1903. { is that ok ??? }
  1904. getmem(newdef,SizeOf(olddef));
  1905. move(olddef^,newdef^,SizeOf(olddef));
  1906. newtype:=new(ptypesym,init(typename,newdef));
  1907. symtablestack^.insert(newtype);
  1908. end
  1909. else
  1910. {$endif testequaltype}
  1911. begin
  1912. getsym(typename,false);
  1913. { check if it is the definition of a forward defined class }
  1914. if assigned(srsym) and (token=_CLASS) and
  1915. (srsym^.typ=typesym) and
  1916. (assigned(ptypesym(srsym)^.definition)) and
  1917. (ptypesym(srsym)^.definition^.deftype=objectdef) and
  1918. ((pobjectdef(ptypesym(srsym)^.definition)^.options and oo_isforward)<>0) and
  1919. ((pobjectdef(ptypesym(srsym)^.definition)^.options and oo_is_class)<>0) then
  1920. begin
  1921. { we can ignore the result }
  1922. { the definition is modified }
  1923. object_dec(typename,pobjectdef(ptypesym(srsym)^.definition));
  1924. newtype:=ptypesym(srsym);
  1925. end
  1926. else
  1927. begin
  1928. newtype:=new(ptypesym,init(typename,read_type(typename)));
  1929. { load newtype with the new pointer to the inserted type
  1930. because it can be an already defined forwarded type !! }
  1931. newtype:=ptypesym(symtablestack^.insert(newtype));
  1932. end;
  1933. end;
  1934. consume(SEMICOLON);
  1935. if assigned(newtype^.definition) and (newtype^.definition^.deftype=procvardef) then
  1936. parse_var_proc_directives(newtype);
  1937. until token<>ID;
  1938. typecanbeforward:=false;
  1939. {$ifdef tp}
  1940. symtablestack^.foreach(testforward_type);
  1941. {$else}
  1942. symtablestack^.foreach(@testforward_type);
  1943. {$endif}
  1944. resolve_forwards;
  1945. block_type:=bt_general;
  1946. end;
  1947. procedure var_dec;
  1948. { parses varaible declarations and inserts them in }
  1949. { the top symbol table of symtablestack }
  1950. begin
  1951. consume(_VAR);
  1952. read_var_decs(false,false);
  1953. end;
  1954. procedure Not_supported_for_inline(t : ttoken);
  1955. begin
  1956. if assigned(aktprocsym) and
  1957. ((aktprocsym^.definition^.options and poinline)<>0) then
  1958. Begin
  1959. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  1960. Message(parser_w_inlining_disabled);
  1961. aktprocsym^.definition^.options:= aktprocsym^.definition^.options and not poinline;
  1962. End;
  1963. end;
  1964. procedure read_declarations(islibrary : boolean);
  1965. begin
  1966. repeat
  1967. case token of
  1968. _LABEL:
  1969. begin
  1970. Not_supported_for_inline(token);
  1971. label_dec;
  1972. end;
  1973. _CONST:
  1974. begin
  1975. Not_supported_for_inline(token);
  1976. const_dec;
  1977. end;
  1978. _TYPE:
  1979. begin
  1980. Not_supported_for_inline(token);
  1981. type_dec;
  1982. end;
  1983. _VAR:
  1984. var_dec;
  1985. _CONSTRUCTOR,_DESTRUCTOR,
  1986. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  1987. begin
  1988. Not_supported_for_inline(token);
  1989. read_proc;
  1990. end;
  1991. _EXPORTS:
  1992. begin
  1993. Not_supported_for_inline(token);
  1994. { here we should be at lexlevel 1, no ? PM }
  1995. if (lexlevel<>main_program_level) or
  1996. (not islibrary and not DLLsource) then
  1997. begin
  1998. Message(parser_e_syntax_error);
  1999. consume_all_until(SEMICOLON);
  2000. end
  2001. else if islibrary then
  2002. read_exports;
  2003. end
  2004. else break;
  2005. end;
  2006. until false;
  2007. end;
  2008. procedure read_interface_declarations;
  2009. begin
  2010. {Since the body is now parsed at lexlevel 1, and the declarations
  2011. must be parsed at the same lexlevel we increase the lexlevel.}
  2012. inc(lexlevel);
  2013. repeat
  2014. case token of
  2015. _CONST : const_dec;
  2016. _TYPE : type_dec;
  2017. _VAR : var_dec;
  2018. _FUNCTION,
  2019. _PROCEDURE,
  2020. _OPERATOR : read_proc;
  2021. else
  2022. break;
  2023. end;
  2024. until false;
  2025. dec(lexlevel);
  2026. end;
  2027. end.
  2028. {
  2029. $Log$
  2030. Revision 1.95 1999-01-25 20:13:48 peter
  2031. * fixed crash with forward declared class of ...
  2032. Revision 1.94 1999/01/19 12:17:00 peter
  2033. * fixed constant strings > 255 chars
  2034. Revision 1.93 1999/01/15 13:08:23 peter
  2035. * error if upper<lower in array decl
  2036. Revision 1.92 1999/01/14 21:49:58 peter
  2037. * fixed forwardpointer problem with multiple forwards for the same
  2038. typesym. It now uses a linkedlist instead of a single pointer
  2039. Revision 1.91 1998/12/30 22:15:46 peter
  2040. + farpointer type
  2041. * absolutesym now also stores if its far
  2042. Revision 1.90 1998/12/15 17:16:00 peter
  2043. * fixed const s : ^string
  2044. * first things for const pchar : @string[1]
  2045. Revision 1.89 1998/12/11 00:03:30 peter
  2046. + globtype,tokens,version unit splitted from globals
  2047. Revision 1.88 1998/11/30 09:43:20 pierre
  2048. * some range check bugs fixed (still not working !)
  2049. + added DLL writing support for win32 (also accepts variables)
  2050. + TempAnsi for code that could be used for Temporary ansi strings
  2051. handling
  2052. Revision 1.87 1998/11/29 12:42:24 peter
  2053. * check for constants with array decl
  2054. Revision 1.86 1998/11/28 16:20:52 peter
  2055. + support for dll variables
  2056. Revision 1.85 1998/11/27 14:34:43 peter
  2057. * give error when string[0] decl is found
  2058. Revision 1.84 1998/11/17 10:40:15 peter
  2059. * H+ fixes
  2060. Revision 1.83 1998/11/16 11:28:59 pierre
  2061. * stackcheck removed for i386_win32
  2062. * exportlist does not crash at least !!
  2063. (was need for tests dir !)z
  2064. Revision 1.82 1998/11/16 10:18:07 peter
  2065. * fixes for ansistrings
  2066. Revision 1.81 1998/11/13 15:40:22 pierre
  2067. + added -Se in Makefile cvstest target
  2068. + lexlevel cleanup
  2069. normal_function_level main_program_level and unit_init_level defined
  2070. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  2071. (test added in code !)
  2072. * -Un option was wrong
  2073. * _FAIL and _SELF only keyword inside
  2074. constructors and methods respectively
  2075. Revision 1.80 1998/11/13 10:18:09 peter
  2076. + nil constants
  2077. Revision 1.79 1998/11/05 12:02:51 peter
  2078. * released useansistring
  2079. * removed -Sv, its now available in fpc modes
  2080. Revision 1.78 1998/10/27 13:45:33 pierre
  2081. * classes get a vmt allways
  2082. * better error info (tried to remove
  2083. several error strings introduced by the tpexcept handling)
  2084. Revision 1.77 1998/10/26 22:58:20 florian
  2085. * new introduded problem with classes fix, the parent class wasn't set
  2086. correct, if the class was defined forward before
  2087. Revision 1.76 1998/10/25 23:31:18 peter
  2088. * procvar parsing updated just like psub.pas routine
  2089. Revision 1.75 1998/10/21 08:39:59 florian
  2090. + ansistring operator +
  2091. + $h and string[n] for n>255 added
  2092. * small problem with TP fixed
  2093. Revision 1.74 1998/10/20 13:09:13 peter
  2094. * fixed object(unknown) crash
  2095. Revision 1.73 1998/10/19 08:54:56 pierre
  2096. * wrong stabs info corrected once again !!
  2097. + variable vmt offset with vmt field only if required
  2098. implemented now !!!
  2099. Revision 1.72 1998/10/16 13:12:51 pierre
  2100. * added vmt_offsets in destructors code also !!!
  2101. * vmt_offset code for m68k
  2102. Revision 1.71 1998/10/15 15:13:25 pierre
  2103. + added oo_hasconstructor and oo_hasdestructor
  2104. for objects options
  2105. Revision 1.70 1998/10/13 13:10:22 peter
  2106. * new style for m68k/i386 infos and enums
  2107. Revision 1.69 1998/10/09 12:07:49 pierre
  2108. * typo error for propertyparas dispose corrected
  2109. Revision 1.68 1998/10/09 11:47:54 pierre
  2110. * still more memory leaks fixes !!
  2111. Revision 1.67 1998/10/08 13:48:46 peter
  2112. * fixed memory leaks for do nothing source
  2113. * fixed unit interdependency
  2114. Revision 1.66 1998/10/06 20:43:31 peter
  2115. * fixed set of bugs. like set of false..true set of #1..#255 and
  2116. set of #1..true which was allowed
  2117. Revision 1.65 1998/10/05 22:43:35 peter
  2118. * commited the wrong file :(
  2119. Revision 1.64 1998/10/05 21:33:24 peter
  2120. * fixed 161,165,166,167,168
  2121. Revision 1.63 1998/10/05 13:57:13 peter
  2122. * crash preventions
  2123. Revision 1.62 1998/10/02 17:06:02 peter
  2124. * better error message for unresolved forward types
  2125. Revision 1.61 1998/10/02 09:23:24 peter
  2126. * fixed error msg with type l=<var>
  2127. * block_type bt_const is now set in read_const_dec
  2128. Revision 1.60 1998/09/30 07:40:33 florian
  2129. * better error recovering
  2130. Revision 1.59 1998/09/26 17:45:33 peter
  2131. + idtoken and only one token table
  2132. Revision 1.58 1998/09/25 00:04:01 florian
  2133. * problems when calling class methods fixed
  2134. Revision 1.57 1998/09/24 23:49:09 peter
  2135. + aktmodeswitches
  2136. Revision 1.56 1998/09/23 15:39:09 pierre
  2137. * browser bugfixes
  2138. was adding a reference when looking for the symbol
  2139. if -bSYM_NAME was used
  2140. Revision 1.55 1998/09/21 13:24:44 daniel
  2141. * Memory leak fixed.
  2142. Revision 1.54 1998/09/17 13:41:16 pierre
  2143. sizeof(TPOINT) problem
  2144. Revision 1.53.2.1 1998/09/17 13:12:09 pierre
  2145. * virtual destructor did not set oo_hasvirtual
  2146. (detected with the sizeof(TPoint) problem
  2147. * genloadcallnode was missing
  2148. Revision 1.53 1998/09/09 11:50:52 pierre
  2149. * forward def are not put in record or objects
  2150. + added check for forwards also in record and objects
  2151. * dummy parasymtable for unit initialization removed from
  2152. symtable stack
  2153. Revision 1.52 1998/09/07 23:10:22 florian
  2154. * a lot of stuff fixed regarding rtti and publishing of properties,
  2155. basics should now work
  2156. Revision 1.51 1998/09/07 19:33:22 florian
  2157. + some stuff for property rtti added:
  2158. - NameIndex of the TPropInfo record is now written correctly
  2159. - the DEFAULT/NODEFAULT keyword is supported now
  2160. - the default value and the storedsym/def are now written to
  2161. the PPU fiel
  2162. Revision 1.50 1998/09/07 18:46:08 peter
  2163. * update smartlinking, uses getdatalabel
  2164. * renamed ptree.value vars to value_str,value_real,value_set
  2165. Revision 1.49 1998/09/07 17:37:00 florian
  2166. * first fixes for published properties
  2167. Revision 1.48 1998/09/04 08:42:02 peter
  2168. * updated some error messages
  2169. Revision 1.47 1998/09/03 16:03:18 florian
  2170. + rtti generation
  2171. * init table generation changed
  2172. Revision 1.46 1998/09/01 17:39:48 peter
  2173. + internal constant functions
  2174. Revision 1.45 1998/08/31 12:20:28 peter
  2175. * fixed array_dec when unknown type was used
  2176. Revision 1.44 1998/08/28 10:57:01 peter
  2177. * removed warnings
  2178. Revision 1.43 1998/08/25 13:09:25 pierre
  2179. * corrected mangling sheme :
  2180. cvar add Cprefix to the mixed case name whereas
  2181. export or public use direct name
  2182. Revision 1.42 1998/08/25 12:42:41 pierre
  2183. * CDECL changed to CVAR for variables
  2184. specifications are read in structures also
  2185. + started adding GPC compatibility mode ( option -Sp)
  2186. * names changed to lowercase
  2187. Revision 1.41 1998/08/23 21:04:36 florian
  2188. + rtti generation for classes added
  2189. + new/dispose do now also a call to INITIALIZE/FINALIZE, if necessaray
  2190. Revision 1.40 1998/08/21 15:48:58 pierre
  2191. * more cdecl chagnes
  2192. - better line info
  2193. - changes the definition options of a procvar
  2194. if it is a unnamed type
  2195. Revision 1.39 1998/08/19 00:42:40 peter
  2196. + subrange types for enums
  2197. + checking for bounds type with ranges
  2198. Revision 1.38 1998/08/12 19:20:39 peter
  2199. + public is the same as export for c_vars
  2200. * a exported/public c_var incs now the refcount
  2201. Revision 1.37 1998/08/11 15:31:38 peter
  2202. * write extended to ppu file
  2203. * new version 0.99.7
  2204. Revision 1.36 1998/08/10 14:50:09 peter
  2205. + localswitches, moduleswitches, globalswitches splitting
  2206. Revision 1.35 1998/07/26 21:59:00 florian
  2207. + better support for switch $H
  2208. + index access to ansi strings added
  2209. + assigment of data (records/arrays) containing ansi strings
  2210. Revision 1.34 1998/07/20 22:17:15 florian
  2211. * hex constants in numeric char (#$54#$43 ...) are now allowed
  2212. * there was a bug in record_var_dec which prevents the used
  2213. of nested variant records (for example drivers.tevent of tv)
  2214. Revision 1.33 1998/07/18 17:11:11 florian
  2215. + ansi string constants fixed
  2216. + switch $H partial implemented
  2217. Revision 1.32 1998/07/14 21:46:50 peter
  2218. * updated messages file
  2219. Revision 1.31 1998/07/14 14:46:53 peter
  2220. * released NEWINPUT
  2221. Revision 1.30 1998/07/10 00:00:00 peter
  2222. * fixed ttypesym bug finally
  2223. * fileinfo in the symtable and better using for unused vars
  2224. Revision 1.29 1998/06/25 14:04:21 peter
  2225. + internal inc/dec
  2226. Revision 1.28 1998/06/24 12:26:45 peter
  2227. * stricter var parsing like tp7 and some optimizes with directive
  2228. parsing
  2229. Revision 1.27 1998/06/12 16:15:34 pierre
  2230. * external name 'C_var';
  2231. export name 'intern_C_var';
  2232. cdecl;
  2233. cdecl;external;
  2234. are now supported only with -Sv switch
  2235. Revision 1.25 1998/06/09 16:01:45 pierre
  2236. + added procedure directive parsing for procvars
  2237. (accepted are popstack cdecl and pascal)
  2238. + added C vars with the following syntax
  2239. var C calias 'true_c_name';(can be followed by external)
  2240. reason is that you must add the Cprefix
  2241. which is target dependent
  2242. Revision 1.24 1998/06/05 14:37:32 pierre
  2243. * fixes for inline for operators
  2244. * inline procedure more correctly restricted
  2245. Revision 1.23 1998/06/04 23:51:50 peter
  2246. * m68k compiles
  2247. + .def file creation moved to gendef.pas so it could also be used
  2248. for win32
  2249. Revision 1.22 1998/06/03 22:48:59 peter
  2250. + wordbool,longbool
  2251. * rename bis,von -> high,low
  2252. * moved some systemunit loading/creating to psystem.pas
  2253. Revision 1.21 1998/06/03 22:14:19 florian
  2254. * problem with sizes of classes fixed (if the anchestor was declared
  2255. forward, the compiler doesn't update the child classes size)
  2256. Revision 1.20 1998/05/28 14:35:54 peter
  2257. * nicer error message when no id is used after var
  2258. Revision 1.19 1998/05/23 01:21:19 peter
  2259. + aktasmmode, aktoptprocessor, aktoutputformat
  2260. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  2261. + $LIBNAME to set the library name where the unit will be put in
  2262. * splitted cgi386 a bit (codeseg to large for bp7)
  2263. * nasm, tasm works again. nasm moved to ag386nsm.pas
  2264. Revision 1.18 1998/05/20 09:42:35 pierre
  2265. + UseTokenInfo now default
  2266. * unit in interface uses and implementation uses gives error now
  2267. * only one error for unknown symbol (uses lastsymknown boolean)
  2268. the problem came from the label code !
  2269. + first inlined procedures and function work
  2270. (warning there might be allowed cases were the result is still wrong !!)
  2271. * UseBrower updated gives a global list of all position of all used symbols
  2272. with switch -gb
  2273. Revision 1.17 1998/05/11 13:07:55 peter
  2274. + $ifdef NEWPPU for the new ppuformat
  2275. + $define GDB not longer required
  2276. * removed all warnings and stripped some log comments
  2277. * no findfirst/findnext anymore to remove smartlink *.o files
  2278. Revision 1.16 1998/05/05 12:05:42 florian
  2279. * problems with properties fixed
  2280. * crash fixed: i:=l when i and l are undefined, was a problem with
  2281. implementation of private/protected
  2282. Revision 1.15 1998/05/01 09:01:23 florian
  2283. + correct semantics of private and protected
  2284. * small fix in variable scope:
  2285. a id can be used in a parameter list of a method, even it is used in
  2286. an anchestor class as field id
  2287. Revision 1.14 1998/05/01 07:43:56 florian
  2288. + basics for rtti implemented
  2289. + switch $m (generate rtti for published sections)
  2290. Revision 1.13 1998/04/30 15:59:41 pierre
  2291. * GDB works again better :
  2292. correct type info in one pass
  2293. + UseTokenInfo for better source position
  2294. * fixed one remaining bug in scanner for line counts
  2295. * several little fixes
  2296. Revision 1.12 1998/04/29 10:33:57 pierre
  2297. + added some code for ansistring (not complete nor working yet)
  2298. * corrected operator overloading
  2299. * corrected nasm output
  2300. + started inline procedures
  2301. + added starstarn : use ** for exponentiation (^ gave problems)
  2302. + started UseTokenInfo cond to get accurate positions
  2303. Revision 1.11 1998/04/28 11:45:52 florian
  2304. * make it compilable with TP
  2305. + small COM problems solved to compile classes.pp
  2306. Revision 1.10 1998/04/27 23:10:28 peter
  2307. + new scanner
  2308. * $makelib -> if smartlink
  2309. * small filename fixes pmodule.setfilename
  2310. * moved import from files.pas -> import.pas
  2311. Revision 1.9 1998/04/10 21:36:56 florian
  2312. + some stuff to support method pointers (procedure of object) added
  2313. (declaration, parameter handling)
  2314. Revision 1.8 1998/04/10 15:39:48 florian
  2315. * more fixes to get classes.pas compiled
  2316. Revision 1.7 1998/04/09 23:02:15 florian
  2317. * small problems solved to get remake3 work
  2318. Revision 1.6 1998/04/09 22:16:35 florian
  2319. * problem with previous REGALLOC solved
  2320. * improved property support
  2321. Revision 1.5 1998/04/08 14:59:20 florian
  2322. * problem with new expr_type solved
  2323. Revision 1.4 1998/04/08 10:26:09 florian
  2324. * correct error handling of virtual constructors
  2325. * problem with new type declaration handling fixed
  2326. Revision 1.3 1998/04/07 22:45:05 florian
  2327. * bug0092, bug0115 and bug0121 fixed
  2328. + packed object/class/array
  2329. Revision 1.2 1998/04/05 13:58:35 peter
  2330. * fixed the -Ss bug
  2331. + warning for Virtual constructors
  2332. * helppages updated with -TGO32V1
  2333. }