psub.pas 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing of the procedures/functions
  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 psub;
  19. interface
  20. uses
  21. cobjects,
  22. symconst,tokens,symtable;
  23. const
  24. pd_global = $1; { directive must be global }
  25. pd_body = $2; { directive needs a body }
  26. pd_implemen = $4; { directive can be used implementation section }
  27. pd_interface = $8; { directive can be used interface section }
  28. pd_object = $10; { directive can be used object declaration }
  29. pd_procvar = $20; { directive can be used procvar declaration }
  30. pd_notobject = $40;{ directive can not be used object declaration }
  31. procedure compile_proc_body(const proc_names:Tstringcontainer;
  32. make_global,parent_has_class:boolean);
  33. procedure parse_proc_head(options:tproctypeoption);
  34. procedure parse_proc_dec;
  35. function is_proc_directive(tok:ttoken):boolean;
  36. procedure parse_var_proc_directives(var sym : psym);
  37. procedure parse_object_proc_directives(var sym : pprocsym);
  38. procedure read_proc;
  39. implementation
  40. uses
  41. globtype,systems,
  42. strings,globals,verbose,files,
  43. scanner,aasm,tree,types,
  44. import,gendef,
  45. {$ifdef newcg}
  46. cgbase,
  47. {$else newcg}
  48. hcodegen,temp_gen,
  49. {$endif newcg}
  50. pass_1,cpubase,cpuasm
  51. {$ifndef NOPASS2}
  52. ,pass_2
  53. {$endif}
  54. {$ifdef GDB}
  55. ,gdb
  56. {$endif GDB}
  57. {$ifdef newcg}
  58. {$ifndef NOOPT}
  59. ,aopt
  60. {$endif}
  61. {$else}
  62. {$ifdef i386}
  63. ,tgeni386
  64. ,cgai386
  65. {$ifndef NOOPT}
  66. ,aopt386
  67. {$endif}
  68. {$endif}
  69. {$ifdef m68k}
  70. ,tgen68k,cga68k
  71. {$endif}
  72. {$endif newcg}
  73. { parser specific stuff }
  74. ,pbase,ptype,pdecl,pexpr,pstatmnt
  75. {$ifdef newcg}
  76. ,tgcpu,convtree,cgobj,tgeni386 { for the new code generator tgeni386 is only a dummy }
  77. {$endif newcg}
  78. ;
  79. var
  80. realname:string; { contains the real name of a procedure as it's typed }
  81. procedure parse_proc_head(options:tproctypeoption);
  82. var sp:stringid;
  83. pd:Pprocdef;
  84. paramoffset:longint;
  85. sym:Psym;
  86. hs:string;
  87. st : psymtable;
  88. overloaded_level:word;
  89. storepos,procstartfilepos : tfileposinfo;
  90. begin
  91. { Save the position where this procedure really starts and set col to 1 which
  92. looks nicer }
  93. procstartfilepos:=tokenpos;
  94. { procstartfilepos.column:=1; I do not agree here !!
  95. lets keep excat position PM }
  96. if (options=potype_operator) then
  97. begin
  98. sp:=overloaded_names[optoken];
  99. realname:=sp;
  100. end
  101. else
  102. begin
  103. sp:=pattern;
  104. realname:=orgpattern;
  105. consume(_ID);
  106. end;
  107. { method ? }
  108. if not(parse_only) and
  109. (lexlevel=normal_function_level) and
  110. try_to_consume(_POINT) then
  111. begin
  112. storepos:=tokenpos;
  113. tokenpos:=procstartfilepos;
  114. getsym(sp,true);
  115. sym:=srsym;
  116. tokenpos:=storepos;
  117. { load proc name }
  118. sp:=pattern;
  119. realname:=orgpattern;
  120. procstartfilepos:=tokenpos;
  121. { qualifier is class name ? }
  122. if (sym^.typ<>typesym) or
  123. (ptypesym(sym)^.restype.def^.deftype<>objectdef) then
  124. begin
  125. Message(parser_e_class_id_expected);
  126. aktprocsym:=nil;
  127. consume(_ID);
  128. end
  129. else
  130. begin
  131. { used to allow private syms to be seen }
  132. aktobjectdef:=pobjectdef(ptypesym(sym)^.restype.def);
  133. procinfo^._class:=pobjectdef(ptypesym(sym)^.restype.def);
  134. aktprocsym:=pprocsym(procinfo^._class^.symtable^.search(sp));
  135. consume(_ID);
  136. {The procedure has been found. So it is
  137. a global one. Set the flags to mark this.}
  138. procinfo^.flags:=procinfo^.flags or pi_is_global;
  139. aktobjectdef:=nil;
  140. { we solve this below }
  141. if not(assigned(aktprocsym)) then
  142. Message(parser_e_methode_id_expected);
  143. end;
  144. end
  145. else
  146. begin
  147. { check for constructor/destructor which is not allowed here }
  148. if (not parse_only) and
  149. (options in [potype_constructor,potype_destructor]) then
  150. Message(parser_e_constructors_always_objects);
  151. tokenpos:=procstartfilepos;
  152. aktprocsym:=pprocsym(symtablestack^.search(sp));
  153. if not(parse_only) then
  154. begin
  155. {The procedure we prepare for is in the implementation
  156. part of the unit we compile. It is also possible that we
  157. are compiling a program, which is also some kind of
  158. implementaion part.
  159. We need to find out if the procedure is global. If it is
  160. global, it is in the global symtable.}
  161. if not assigned(aktprocsym) and
  162. (symtablestack^.symtabletype=staticsymtable) then
  163. begin
  164. {Search the procedure in the global symtable.}
  165. aktprocsym:=Pprocsym(search_a_symtable(sp,globalsymtable));
  166. if assigned(aktprocsym) then
  167. begin
  168. {Check if it is a procedure.}
  169. if aktprocsym^.typ<>procsym then
  170. DuplicateSym(aktprocsym);
  171. {The procedure has been found. So it is
  172. a global one. Set the flags to mark this.}
  173. procinfo^.flags:=procinfo^.flags or pi_is_global;
  174. end;
  175. end;
  176. end;
  177. end;
  178. { Create the mangledname }
  179. {$ifndef UseNiceNames}
  180. if assigned(procinfo^._class) then
  181. begin
  182. if (pos('_$$_',procprefix)=0) then
  183. hs:=procprefix+'_$$_'+procinfo^._class^.objname^+'_$$_'+sp
  184. else
  185. hs:=procprefix+'_$'+sp;
  186. end
  187. else
  188. begin
  189. if lexlevel=normal_function_level then
  190. hs:=procprefix+'_'+sp
  191. else
  192. hs:=procprefix+'_$'+sp;
  193. end;
  194. {$else UseNiceNames}
  195. if assigned(procinfo^._class) then
  196. begin
  197. if (pos('_5Class_',procprefix)=0) then
  198. hs:=procprefix+'_5Class_'+procinfo^._class^.name^+'_'+tostr(length(sp))+sp
  199. else
  200. hs:=procprefix+'_'+tostr(length(sp))+sp;
  201. end
  202. else
  203. begin
  204. if lexlevel=normal_function_level then
  205. hs:=procprefix+'_'+tostr(length(sp))+sp
  206. else
  207. hs:=lowercase(procprefix)+'_'+tostr(length(sp))+sp;
  208. end;
  209. {$endif UseNiceNames}
  210. if assigned(aktprocsym) then
  211. begin
  212. { Check if overloading is enabled }
  213. if not(m_fpc in aktmodeswitches) then
  214. begin
  215. if aktprocsym^.typ<>procsym then
  216. begin
  217. DuplicateSym(aktprocsym);
  218. { try to recover by creating a new aktprocsym }
  219. tokenpos:=procstartfilepos;
  220. aktprocsym:=new(pprocsym,init(sp));
  221. end
  222. else
  223. begin
  224. if not(aktprocsym^.definition^.forwarddef) then
  225. Message(parser_e_procedure_overloading_is_off);
  226. end;
  227. end
  228. else
  229. begin
  230. { Check if the overloaded sym is realy a procsym }
  231. if aktprocsym^.typ<>procsym then
  232. begin
  233. Message1(parser_e_overloaded_no_procedure,aktprocsym^.name);
  234. { try to recover by creating a new aktprocsym }
  235. tokenpos:=procstartfilepos;
  236. aktprocsym:=new(pprocsym,init(sp));
  237. end;
  238. end;
  239. end
  240. else
  241. begin
  242. { create a new procsym and set the real filepos }
  243. tokenpos:=procstartfilepos;
  244. aktprocsym:=new(pprocsym,init(sp));
  245. { for operator we have only one definition for each overloaded
  246. operation }
  247. if (options=potype_operator) then
  248. begin
  249. { the only problem is that nextoverloaded might not be in a unit
  250. known for the unit itself }
  251. if assigned(overloaded_operators[optoken]) then
  252. aktprocsym^.definition:=overloaded_operators[optoken]^.definition;
  253. end;
  254. symtablestack^.insert(aktprocsym);
  255. end;
  256. st:=symtablestack;
  257. pd:=new(pprocdef,init);
  258. pd^.symtablelevel:=symtablestack^.symtablelevel;
  259. if assigned(procinfo^._class) then
  260. pd^._class := procinfo^._class;
  261. { set the options from the caller (podestructor or poconstructor) }
  262. pd^.proctypeoption:=options;
  263. { calculate the offset of the parameters }
  264. paramoffset:=8;
  265. { calculate frame pointer offset }
  266. if lexlevel>normal_function_level then
  267. begin
  268. procinfo^.framepointer_offset:=paramoffset;
  269. inc(paramoffset,target_os.size_of_pointer);
  270. { this is needed to get correct framepointer push for local
  271. forward functions !! }
  272. pd^.parast^.symtablelevel:=lexlevel;
  273. end;
  274. if assigned (procinfo^._Class) and
  275. not(procinfo^._Class^.is_class) and
  276. (pd^.proctypeoption in [potype_constructor,potype_destructor]) then
  277. inc(paramoffset,target_os.size_of_pointer);
  278. { self pointer offset }
  279. { self isn't pushed in nested procedure of methods }
  280. if assigned(procinfo^._class) and (lexlevel=normal_function_level) then
  281. begin
  282. procinfo^.selfpointer_offset:=paramoffset;
  283. if assigned(aktprocsym^.definition) and
  284. not(po_containsself in aktprocsym^.definition^.procoptions) then
  285. inc(paramoffset,target_os.size_of_pointer);
  286. end;
  287. { con/-destructor flag ? }
  288. if assigned (procinfo^._Class) and
  289. procinfo^._class^.is_class and
  290. (pd^.proctypeoption in [potype_destructor,potype_constructor]) then
  291. inc(paramoffset,target_os.size_of_pointer);
  292. procinfo^.para_offset:=paramoffset;
  293. pd^.parast^.datasize:=0;
  294. pd^.nextoverloaded:=aktprocsym^.definition;
  295. aktprocsym^.definition:=pd;
  296. { this is probably obsolete now PM }
  297. aktprocsym^.definition^.fileinfo:=procstartfilepos;
  298. aktprocsym^.definition^.setmangledname(hs);
  299. aktprocsym^.definition^.procsym:=aktprocsym;
  300. if not parse_only then
  301. begin
  302. overloaded_level:=0;
  303. { we need another procprefix !!! }
  304. { count, but only those in the same unit !!}
  305. while assigned(pd) and
  306. (pd^.owner^.symtabletype in [globalsymtable,staticsymtable]) do
  307. begin
  308. { only count already implemented functions }
  309. if not(pd^.forwarddef) then
  310. inc(overloaded_level);
  311. pd:=pd^.nextoverloaded;
  312. end;
  313. if overloaded_level>0 then
  314. procprefix:=hs+'$'+tostr(overloaded_level)+'$'
  315. else
  316. procprefix:=hs+'$';
  317. end;
  318. { this must also be inserted in the right symtable !! PM }
  319. { otherwise we get subbtle problems with
  320. definitions of args defs in staticsymtable for
  321. implementation of a global method }
  322. if token=_LKLAMMER then
  323. parameter_dec(aktprocsym^.definition);
  324. { so we only restore the symtable now }
  325. symtablestack:=st;
  326. if (options=potype_operator) then
  327. overloaded_operators[optoken]:=aktprocsym;
  328. end;
  329. procedure parse_proc_dec;
  330. var
  331. hs : string;
  332. isclassmethod : boolean;
  333. begin
  334. inc(lexlevel);
  335. { read class method }
  336. if token=_CLASS then
  337. begin
  338. consume(_CLASS);
  339. isclassmethod:=true;
  340. end
  341. else
  342. isclassmethod:=false;
  343. case token of
  344. _FUNCTION : begin
  345. consume(_FUNCTION);
  346. parse_proc_head(potype_none);
  347. if token<>_COLON then
  348. begin
  349. if not(aktprocsym^.definition^.forwarddef) or
  350. (m_repeat_forward in aktmodeswitches) then
  351. begin
  352. consume(_COLON);
  353. consume_all_until(_SEMICOLON);
  354. end;
  355. end
  356. else
  357. begin
  358. consume(_COLON);
  359. inc(testcurobject);
  360. single_type(aktprocsym^.definition^.rettype,hs,false);
  361. aktprocsym^.definition^.test_if_fpu_result;
  362. dec(testcurobject);
  363. end;
  364. end;
  365. _PROCEDURE : begin
  366. consume(_PROCEDURE);
  367. parse_proc_head(potype_none);
  368. aktprocsym^.definition^.rettype.def:=voiddef;
  369. end;
  370. _CONSTRUCTOR : begin
  371. consume(_CONSTRUCTOR);
  372. parse_proc_head(potype_constructor);
  373. if assigned(procinfo^._class) and
  374. procinfo^._class^.is_class then
  375. begin
  376. { CLASS constructors return the created instance }
  377. aktprocsym^.definition^.rettype.def:=procinfo^._class;
  378. end
  379. else
  380. begin
  381. { OBJECT constructors return a boolean }
  382. {$IfDef GDB}
  383. { GDB doesn't like unnamed types !}
  384. aktprocsym^.definition^.rettype.def:=globaldef('boolean');
  385. {$else GDB}
  386. aktprocsym^.definition^.rettype.def:=new(porddef,init(bool8bit,0,1));
  387. {$Endif GDB}
  388. end;
  389. end;
  390. _DESTRUCTOR : begin
  391. consume(_DESTRUCTOR);
  392. parse_proc_head(potype_destructor);
  393. aktprocsym^.definition^.rettype.def:=voiddef;
  394. end;
  395. _OPERATOR : begin
  396. if lexlevel>normal_function_level then
  397. Message(parser_e_no_local_operator);
  398. consume(_OPERATOR);
  399. if not(token in [_PLUS..last_overloaded]) then
  400. Message(parser_e_overload_operator_failed);
  401. optoken:=token;
  402. consume(Token);
  403. procinfo^.flags:=procinfo^.flags or pi_operator;
  404. parse_proc_head(potype_operator);
  405. if token<>_ID then
  406. begin
  407. opsym:=nil;
  408. if not(m_result in aktmodeswitches) then
  409. consume(_ID);
  410. end
  411. else
  412. begin
  413. opsym:=new(pvarsym,initdef(pattern,voiddef));
  414. consume(_ID);
  415. end;
  416. if not try_to_consume(_COLON) then
  417. begin
  418. consume(_COLON);
  419. aktprocsym^.definition^.rettype.def:=generrordef;
  420. consume_all_until(_SEMICOLON);
  421. end
  422. else
  423. begin
  424. single_type(aktprocsym^.definition^.rettype,hs,false);
  425. aktprocsym^.definition^.test_if_fpu_result;
  426. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  427. ((aktprocsym^.definition^.rettype.def^.deftype<>
  428. orddef) or (porddef(aktprocsym^.definition^.
  429. rettype.def)^.typ<>bool8bit)) then
  430. Message(parser_e_comparative_operator_return_boolean);
  431. if assigned(opsym) then
  432. opsym^.vartype.def:=aktprocsym^.definition^.rettype.def;
  433. { We need to add the return type in the mangledname
  434. to allow overloading with just different results !! (PM) }
  435. aktprocsym^.definition^.setmangledname(
  436. aktprocsym^.definition^.mangledname+'$$'+hs);
  437. if (optoken=_ASSIGNMENT) and
  438. is_equal(aktprocsym^.definition^.rettype.def,
  439. pvarsym(aktprocsym^.definition^.parast^.symindex^.first)^.vartype.def) then
  440. message(parser_e_no_such_assignment);
  441. end;
  442. end;
  443. end;
  444. if isclassmethod and
  445. assigned(aktprocsym) then
  446. {$ifdef INCLUDEOK}
  447. include(aktprocsym^.definition^.procoptions,po_classmethod);
  448. {$else}
  449. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_classmethod];
  450. {$endif}
  451. consume(_SEMICOLON);
  452. dec(lexlevel);
  453. end;
  454. {****************************************************************************
  455. Procedure directive handlers
  456. ****************************************************************************}
  457. {$ifdef tp}
  458. {$F+}
  459. {$endif}
  460. procedure pd_far(const procnames:Tstringcontainer);
  461. begin
  462. Message(parser_w_proc_far_ignored);
  463. end;
  464. procedure pd_near(const procnames:Tstringcontainer);
  465. begin
  466. Message(parser_w_proc_near_ignored);
  467. end;
  468. procedure pd_export(const procnames:Tstringcontainer);
  469. begin
  470. if assigned(procinfo^._class) then
  471. Message(parser_e_methods_dont_be_export);
  472. if lexlevel<>normal_function_level then
  473. Message(parser_e_dont_nest_export);
  474. { only os/2 needs this }
  475. if target_info.target=target_i386_os2 then
  476. begin
  477. procnames.insert(realname);
  478. procinfo^.exported:=true;
  479. if cs_link_deffile in aktglobalswitches then
  480. deffile.AddExport(aktprocsym^.definition^.mangledname);
  481. end;
  482. end;
  483. procedure pd_inline(const procnames:Tstringcontainer);
  484. begin
  485. if not(cs_support_inline in aktmoduleswitches) then
  486. Message(parser_e_proc_inline_not_supported);
  487. end;
  488. procedure pd_forward(const procnames:Tstringcontainer);
  489. begin
  490. aktprocsym^.definition^.forwarddef:=true;
  491. end;
  492. procedure pd_stdcall(const procnames:Tstringcontainer);
  493. begin
  494. end;
  495. procedure pd_safecall(const procnames:Tstringcontainer);
  496. begin
  497. end;
  498. procedure pd_alias(const procnames:Tstringcontainer);
  499. begin
  500. consume(_COLON);
  501. procnames.insert(get_stringconst);
  502. end;
  503. procedure pd_asmname(const procnames:Tstringcontainer);
  504. begin
  505. aktprocsym^.definition^.setmangledname(target_os.Cprefix+pattern);
  506. if token=_CCHAR then
  507. consume(_CCHAR)
  508. else
  509. consume(_CSTRING);
  510. { we don't need anything else }
  511. aktprocsym^.definition^.forwarddef:=false;
  512. end;
  513. procedure pd_intern(const procnames:Tstringcontainer);
  514. begin
  515. consume(_COLON);
  516. aktprocsym^.definition^.extnumber:=get_intconst;
  517. end;
  518. procedure pd_system(const procnames:Tstringcontainer);
  519. begin
  520. aktprocsym^.definition^.setmangledname(realname);
  521. end;
  522. procedure pd_abstract(const procnames:Tstringcontainer);
  523. begin
  524. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  525. {$ifdef INCLUDEOK}
  526. include(aktprocsym^.definition^.procoptions,po_abstractmethod)
  527. {$else}
  528. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_abstractmethod]
  529. {$endif}
  530. else
  531. Message(parser_e_only_virtual_methods_abstract);
  532. { the method is defined }
  533. aktprocsym^.definition^.forwarddef:=false;
  534. end;
  535. procedure pd_virtual(const procnames:Tstringcontainer);
  536. {$ifdef WITHDMT}
  537. var
  538. pt : ptree;
  539. {$endif WITHDMT}
  540. begin
  541. if (aktprocsym^.definition^.proctypeoption=potype_constructor) and
  542. not(aktprocsym^.definition^._class^.is_class) then
  543. Message(parser_e_constructor_cannot_be_not_virtual);
  544. {$ifdef WITHDMT}
  545. if not(aktprocsym^.definition^._class^.is_class) and
  546. (token<>_SEMICOLON) then
  547. begin
  548. { any type of parameter is allowed here! }
  549. pt:=comp_expr(true);
  550. do_firstpass(pt);
  551. if is_constintnode(pt) then
  552. begin
  553. include(aktprocsym^.definition^.procoptions,po_msgint);
  554. aktprocsym^.definition^.messageinf.i:=pt^.value;
  555. end
  556. else
  557. Message(parser_e_ill_msg_expr);
  558. disposetree(pt);
  559. end;
  560. {$endif WITHDMT}
  561. end;
  562. procedure pd_static(const procnames:Tstringcontainer);
  563. begin
  564. if (cs_static_keyword in aktmoduleswitches) then
  565. begin
  566. {$ifdef INCLUDEOK}
  567. include(aktprocsym^.symoptions,sp_static);
  568. include(aktprocsym^.definition^.procoptions,po_staticmethod);
  569. {$else}
  570. aktprocsym^.symoptions:=aktprocsym^.symoptions+[sp_static];
  571. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_staticmethod];
  572. {$endif}
  573. end;
  574. end;
  575. procedure pd_override(const procnames:Tstringcontainer);
  576. begin
  577. if not(aktprocsym^.definition^._class^.is_class) then
  578. Message(parser_e_no_object_override);
  579. end;
  580. procedure pd_message(const procnames:Tstringcontainer);
  581. var
  582. pt : ptree;
  583. begin
  584. { check parameter type }
  585. if not(po_containsself in aktprocsym^.definition^.procoptions) and
  586. ((aktprocsym^.definition^.para^.count<>1) or
  587. (pparaitem(aktprocsym^.definition^.para^.first)^.paratyp<>vs_var)) then
  588. Message(parser_e_ill_msg_param);
  589. pt:=comp_expr(true);
  590. do_firstpass(pt);
  591. if pt^.treetype=stringconstn then
  592. begin
  593. {$ifdef INCLUDEOK}
  594. include(aktprocsym^.definition^.procoptions,po_msgstr);
  595. {$else}
  596. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgstr];
  597. {$endif}
  598. aktprocsym^.definition^.messageinf.str:=strnew(pt^.value_str);
  599. end
  600. else
  601. if is_constintnode(pt) then
  602. begin
  603. {$ifdef INCLUDEOK}
  604. include(aktprocsym^.definition^.procoptions,po_msgint);
  605. {$else}
  606. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgint];
  607. {$endif}
  608. aktprocsym^.definition^.messageinf.i:=pt^.value;
  609. end
  610. else
  611. Message(parser_e_ill_msg_expr);
  612. disposetree(pt);
  613. end;
  614. procedure pd_cdecl(const procnames:Tstringcontainer);
  615. begin
  616. if aktprocsym^.definition^.deftype<>procvardef then
  617. aktprocsym^.definition^.setmangledname(target_os.Cprefix+realname);
  618. end;
  619. procedure pd_pascal(const procnames:Tstringcontainer);
  620. var st,parast : psymtable;
  621. lastps,ps : psym;
  622. begin
  623. new(st,init(parasymtable));
  624. parast:=aktprocsym^.definition^.parast;
  625. lastps:=nil;
  626. while assigned(parast^.symindex^.first) and (lastps<>psym(parast^.symindex^.first)) do
  627. begin
  628. ps:=psym(parast^.symindex^.first);
  629. while assigned(ps^.next) and (psym(ps^.next)<>lastps) do
  630. ps:=psym(ps^.next);
  631. ps^.owner:=st;
  632. { recalculate the corrected offset }
  633. { the really_insert_in_data procedure
  634. for parasymtable should only calculateoffset PM }
  635. ps^.insert_in_data;
  636. { reset the owner correctly }
  637. ps^.owner:=parast;
  638. lastps:=ps;
  639. end;
  640. end;
  641. procedure pd_register(const procnames:Tstringcontainer);
  642. begin
  643. Message(parser_w_proc_register_ignored);
  644. end;
  645. procedure pd_syscall(const procnames:Tstringcontainer);
  646. begin
  647. aktprocsym^.definition^.forwarddef:=false;
  648. aktprocsym^.definition^.extnumber:=get_intconst;
  649. end;
  650. procedure pd_external(const procnames:Tstringcontainer);
  651. {
  652. If import_dll=nil the procedure is assumed to be in another
  653. object file. In that object file it should have the name to
  654. which import_name is pointing to. Otherwise, the procedure is
  655. assumed to be in the DLL to which import_dll is pointing to. In
  656. that case either import_nr<>0 or import_name<>nil is true, so
  657. the procedure is either imported by number or by name. (DM)
  658. }
  659. var
  660. import_dll,
  661. import_name : string;
  662. import_nr : word;
  663. begin
  664. aktprocsym^.definition^.forwarddef:=false;
  665. { If the procedure should be imported from a DLL, a constant string follows.
  666. This isn't really correct, an contant string expression follows
  667. so we check if an semicolon follows, else a string constant have to
  668. follow (FK) }
  669. import_nr:=0;
  670. import_name:='';
  671. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  672. begin
  673. import_dll:=get_stringconst;
  674. if (idtoken=_NAME) then
  675. begin
  676. consume(_NAME);
  677. import_name:=get_stringconst;
  678. end;
  679. if (idtoken=_INDEX) then
  680. begin
  681. {After the word index follows the index number in the DLL.}
  682. consume(_INDEX);
  683. import_nr:=get_intconst;
  684. end;
  685. if (import_nr=0) and (import_name='') then
  686. {if (aktprocsym^.definition^.options and pocdecl)<>0 then
  687. import_name:=aktprocsym^.definition^.mangledname
  688. else
  689. Message(parser_w_empty_import_name);}
  690. { this should work both for win32 and Linux !! PM }
  691. import_name:=realname;
  692. if not(current_module^.uses_imports) then
  693. begin
  694. current_module^.uses_imports:=true;
  695. importlib^.preparelib(current_module^.modulename^);
  696. end;
  697. if not(m_repeat_forward in aktmodeswitches) then
  698. begin
  699. { we can only have one overloaded here ! }
  700. if assigned(aktprocsym^.definition^.nextoverloaded) then
  701. importlib^.importprocedure(aktprocsym^.definition^.nextoverloaded^.mangledname,
  702. import_dll,import_nr,import_name)
  703. else
  704. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  705. end
  706. else
  707. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  708. end
  709. else
  710. begin
  711. if (idtoken=_NAME) then
  712. begin
  713. consume(_NAME);
  714. import_name:=get_stringconst;
  715. aktprocsym^.definition^.setmangledname(import_name);
  716. end
  717. else
  718. begin
  719. { external shouldn't override the cdecl/system name }
  720. if not (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  721. aktprocsym^.definition^.setmangledname(aktprocsym^.name);
  722. end;
  723. end;
  724. end;
  725. {$ifdef TP}
  726. {$F-}
  727. {$endif}
  728. {const
  729. namelength=15;}
  730. type
  731. pd_handler=procedure(const procnames:Tstringcontainer);
  732. proc_dir_rec=record
  733. idtok : ttoken;
  734. pd_flags : longint;
  735. handler : pd_handler;
  736. pocall : tproccalloptions;
  737. pooption : tprocoptions;
  738. mutexclpocall : tproccalloptions;
  739. mutexclpotype : tproctypeoptions;
  740. mutexclpo : tprocoptions;
  741. end;
  742. const
  743. {Should contain the number of procedure directives we support.}
  744. num_proc_directives=29;
  745. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  746. (
  747. (
  748. idtok:_ABSTRACT;
  749. pd_flags : pd_interface+pd_object;
  750. handler : {$ifndef TP}@{$endif}pd_abstract;
  751. pocall : [];
  752. pooption : [po_abstractmethod];
  753. mutexclpocall : [pocall_internproc,pocall_inline];
  754. mutexclpotype : [potype_constructor,potype_destructor];
  755. mutexclpo : [po_exports,po_interrupt,po_external]
  756. ),(
  757. idtok:_ALIAS;
  758. pd_flags : pd_implemen+pd_body;
  759. handler : {$ifndef TP}@{$endif}pd_alias;
  760. pocall : [];
  761. pooption : [];
  762. mutexclpocall : [pocall_inline];
  763. mutexclpotype : [];
  764. mutexclpo : [po_external]
  765. ),(
  766. idtok:_ASMNAME;
  767. pd_flags : pd_interface+pd_implemen;
  768. handler : {$ifndef TP}@{$endif}pd_asmname;
  769. pocall : [pocall_cdecl,pocall_clearstack];
  770. pooption : [po_external];
  771. mutexclpocall : [pocall_internproc];
  772. mutexclpotype : [];
  773. mutexclpo : [po_external]
  774. ),(
  775. idtok:_ASSEMBLER;
  776. pd_flags : pd_implemen+pd_body;
  777. handler : nil;
  778. pocall : [];
  779. pooption : [po_assembler];
  780. mutexclpocall : [];
  781. mutexclpotype : [];
  782. mutexclpo : [po_external]
  783. ),(
  784. idtok:_CDECL;
  785. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  786. handler : {$ifndef TP}@{$endif}pd_cdecl;
  787. pocall : [pocall_cdecl,pocall_clearstack];
  788. pooption : [po_savestdregs];
  789. mutexclpocall : [pocall_internproc,pocall_leftright,pocall_inline];
  790. mutexclpotype : [];
  791. mutexclpo : [po_assembler,po_external]
  792. ),(
  793. idtok:_DYNAMIC;
  794. pd_flags : pd_interface+pd_object;
  795. handler : {$ifndef TP}@{$endif}pd_virtual;
  796. pocall : [];
  797. pooption : [po_virtualmethod];
  798. mutexclpocall : [pocall_internproc,pocall_inline];
  799. mutexclpotype : [];
  800. mutexclpo : [po_exports,po_interrupt,po_external]
  801. ),(
  802. idtok:_EXPORT;
  803. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??};
  804. handler : {$ifndef TP}@{$endif}pd_export;
  805. pocall : [];
  806. pooption : [po_exports];
  807. mutexclpocall : [pocall_internproc,pocall_inline];
  808. mutexclpotype : [];
  809. mutexclpo : [po_external,po_interrupt]
  810. ),(
  811. idtok:_EXTERNAL;
  812. pd_flags : pd_implemen+pd_interface;
  813. handler : {$ifndef TP}@{$endif}pd_external;
  814. pocall : [];
  815. pooption : [po_external];
  816. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  817. mutexclpotype : [];
  818. mutexclpo : [po_exports,po_interrupt,po_assembler]
  819. ),(
  820. idtok:_FAR;
  821. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar;
  822. handler : {$ifndef TP}@{$endif}pd_far;
  823. pocall : [];
  824. pooption : [];
  825. mutexclpocall : [pocall_internproc,pocall_inline];
  826. mutexclpotype : [];
  827. mutexclpo : []
  828. ),(
  829. idtok:_FORWARD;
  830. pd_flags : pd_implemen;
  831. handler : {$ifndef TP}@{$endif}pd_forward;
  832. pocall : [];
  833. pooption : [];
  834. mutexclpocall : [pocall_internproc,pocall_inline];
  835. mutexclpotype : [];
  836. mutexclpo : [po_external]
  837. ),(
  838. idtok:_INLINE;
  839. pd_flags : pd_implemen+pd_body;
  840. handler : {$ifndef TP}@{$endif}pd_inline;
  841. pocall : [pocall_inline];
  842. pooption : [];
  843. mutexclpocall : [pocall_internproc];
  844. mutexclpotype : [potype_constructor,potype_destructor];
  845. mutexclpo : [po_exports,po_external,po_interrupt]
  846. ),(
  847. idtok:_INTERNCONST;
  848. pd_flags : pd_implemen+pd_body;
  849. handler : {$ifndef TP}@{$endif}pd_intern;
  850. pocall : [pocall_internconst];
  851. pooption : [];
  852. mutexclpocall : [];
  853. mutexclpotype : [potype_operator];
  854. mutexclpo : []
  855. ),(
  856. idtok:_INTERNPROC;
  857. pd_flags : pd_implemen;
  858. handler : {$ifndef TP}@{$endif}pd_intern;
  859. pocall : [pocall_internproc];
  860. pooption : [];
  861. mutexclpocall : [pocall_inline,pocall_clearstack,pocall_leftright,pocall_cdecl];
  862. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  863. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck]
  864. ),(
  865. idtok:_INTERRUPT;
  866. pd_flags : pd_implemen+pd_body;
  867. handler : nil;
  868. pocall : [];
  869. pooption : [po_interrupt];
  870. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_clearstack,pocall_leftright,pocall_inline];
  871. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  872. mutexclpo : [po_external]
  873. ),(
  874. idtok:_IOCHECK;
  875. pd_flags : pd_implemen+pd_body;
  876. handler : nil;
  877. pocall : [];
  878. pooption : [po_iocheck];
  879. mutexclpocall : [pocall_internproc];
  880. mutexclpotype : [];
  881. mutexclpo : [po_external]
  882. ),(
  883. idtok:_MESSAGE;
  884. pd_flags : pd_interface+pd_object;
  885. handler : {$ifndef TP}@{$endif}pd_message;
  886. pocall : [];
  887. pooption : []; { can be po_msgstr or po_msgint }
  888. mutexclpocall : [pocall_inline,pocall_internproc];
  889. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  890. mutexclpo : [po_interrupt,po_external]
  891. ),(
  892. idtok:_NEAR;
  893. pd_flags : pd_implemen+pd_body+pd_procvar;
  894. handler : {$ifndef TP}@{$endif}pd_near;
  895. pocall : [];
  896. pooption : [];
  897. mutexclpocall : [pocall_internproc];
  898. mutexclpotype : [];
  899. mutexclpo : []
  900. ),(
  901. idtok:_OVERRIDE;
  902. pd_flags : pd_interface+pd_object;
  903. handler : {$ifndef TP}@{$endif}pd_override;
  904. pocall : [];
  905. pooption : [po_overridingmethod,po_virtualmethod];
  906. mutexclpocall : [pocall_inline,pocall_internproc];
  907. mutexclpotype : [];
  908. mutexclpo : [po_exports,po_external,po_interrupt]
  909. ),(
  910. idtok:_PASCAL;
  911. pd_flags : pd_implemen+pd_body+pd_procvar;
  912. handler : {$ifndef TP}@{$endif}pd_pascal;
  913. pocall : [pocall_leftright];
  914. pooption : [];
  915. mutexclpocall : [pocall_internproc];
  916. mutexclpotype : [];
  917. mutexclpo : [po_external]
  918. ),(
  919. idtok:_POPSTACK;
  920. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  921. handler : nil;
  922. pocall : [pocall_clearstack];
  923. pooption : [];
  924. mutexclpocall : [pocall_inline,pocall_internproc];
  925. mutexclpotype : [];
  926. mutexclpo : [po_assembler,po_external]
  927. ),(
  928. idtok:_PUBLIC;
  929. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject;
  930. handler : nil;
  931. pocall : [];
  932. pooption : [];
  933. mutexclpocall : [pocall_internproc,pocall_inline];
  934. mutexclpotype : [];
  935. mutexclpo : [po_external]
  936. ),(
  937. idtok:_REGISTER;
  938. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  939. handler : {$ifndef TP}@{$endif}pd_register;
  940. pocall : [pocall_register];
  941. pooption : [];
  942. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc];
  943. mutexclpotype : [];
  944. mutexclpo : [po_external]
  945. ),(
  946. idtok:_SAFECALL;
  947. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  948. handler : {$ifndef TP}@{$endif}pd_safecall;
  949. pocall : [pocall_safecall];
  950. pooption : [po_savestdregs];
  951. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc,pocall_inline];
  952. mutexclpotype : [];
  953. mutexclpo : [po_external]
  954. ),(
  955. idtok:_SAVEREGISTERS;
  956. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  957. handler : nil;
  958. pocall : [];
  959. pooption : [po_saveregisters];
  960. mutexclpocall : [pocall_internproc];
  961. mutexclpotype : [];
  962. mutexclpo : [po_external]
  963. ),(
  964. idtok:_STATIC;
  965. pd_flags : pd_interface+pd_object;
  966. handler : {$ifndef TP}@{$endif}pd_static;
  967. pocall : [];
  968. pooption : [po_staticmethod];
  969. mutexclpocall : [pocall_inline,pocall_internproc];
  970. mutexclpotype : [potype_constructor,potype_destructor];
  971. mutexclpo : [po_external,po_interrupt,po_exports]
  972. ),(
  973. idtok:_STDCALL;
  974. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  975. handler : {$ifndef TP}@{$endif}pd_stdcall;
  976. pocall : [pocall_stdcall];
  977. pooption : [po_savestdregs];
  978. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_inline,pocall_internproc];
  979. mutexclpotype : [];
  980. mutexclpo : [po_external]
  981. ),(
  982. idtok:_SYSCALL;
  983. pd_flags : pd_interface;
  984. handler : {$ifndef TP}@{$endif}pd_syscall;
  985. pocall : [pocall_palmossyscall];
  986. pooption : [];
  987. mutexclpocall : [pocall_cdecl,pocall_inline,pocall_internproc];
  988. mutexclpotype : [];
  989. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  990. ),(
  991. idtok:_SYSTEM;
  992. pd_flags : pd_implemen;
  993. handler : {$ifndef TP}@{$endif}pd_system;
  994. pocall : [pocall_clearstack];
  995. pooption : [];
  996. mutexclpocall : [pocall_leftright,pocall_inline,pocall_internproc];
  997. mutexclpotype : [];
  998. mutexclpo : [po_external,po_assembler,po_interrupt]
  999. ),(
  1000. idtok:_VIRTUAL;
  1001. pd_flags : pd_interface+pd_object;
  1002. handler : {$ifndef TP}@{$endif}pd_virtual;
  1003. pocall : [];
  1004. pooption : [po_virtualmethod];
  1005. mutexclpocall : [pocall_inline,pocall_internproc];
  1006. mutexclpotype : [];
  1007. mutexclpo : [po_external,po_interrupt,po_exports]
  1008. )
  1009. );
  1010. function is_proc_directive(tok:ttoken):boolean;
  1011. var
  1012. i : longint;
  1013. begin
  1014. is_proc_directive:=false;
  1015. for i:=1 to num_proc_directives do
  1016. if proc_direcdata[i].idtok=idtoken then
  1017. begin
  1018. is_proc_directive:=true;
  1019. exit;
  1020. end;
  1021. end;
  1022. function parse_proc_direc(const proc_names:Tstringcontainer;var pdflags:word):boolean;
  1023. {
  1024. Parse the procedure directive, returns true if a correct directive is found
  1025. }
  1026. var
  1027. p : longint;
  1028. found : boolean;
  1029. name : string;
  1030. begin
  1031. parse_proc_direc:=false;
  1032. name:=pattern;
  1033. found:=false;
  1034. for p:=1 to num_proc_directives do
  1035. if proc_direcdata[p].idtok=idtoken then
  1036. begin
  1037. found:=true;
  1038. break;
  1039. end;
  1040. { Check if the procedure directive is known }
  1041. if not found then
  1042. begin
  1043. { parsing a procvar type the name can be any
  1044. next variable !! }
  1045. if (pdflags and (pd_procvar or pd_object))=0 then
  1046. Message1(parser_w_unknown_proc_directive_ignored,name);
  1047. exit;
  1048. end;
  1049. { static needs a special treatment }
  1050. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1051. exit;
  1052. { Conflicts between directives ? }
  1053. if (aktprocsym^.definition^.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1054. ((aktprocsym^.definition^.proccalloptions*proc_direcdata[p].mutexclpocall)<>[]) or
  1055. ((aktprocsym^.definition^.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1056. begin
  1057. Message1(parser_e_proc_dir_conflict,name);
  1058. exit;
  1059. end;
  1060. { Check if the directive is only for objects }
  1061. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1062. not assigned(aktprocsym^.definition^._class) then
  1063. begin
  1064. exit;
  1065. end;
  1066. { check if method and directive not for object public }
  1067. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1068. assigned(aktprocsym^.definition^._class) then
  1069. begin
  1070. exit;
  1071. end;
  1072. { consume directive, and turn flag on }
  1073. consume(token);
  1074. parse_proc_direc:=true;
  1075. { Check the pd_flags if the directive should be allowed }
  1076. if ((pdflags and pd_interface)<>0) and
  1077. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1078. begin
  1079. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1080. exit;
  1081. end;
  1082. if ((pdflags and pd_implemen)<>0) and
  1083. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1084. begin
  1085. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1086. exit;
  1087. end;
  1088. if ((pdflags and pd_procvar)<>0) and
  1089. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1090. begin
  1091. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1092. exit;
  1093. end;
  1094. { Return the new pd_flags }
  1095. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1096. pdflags:=pdflags and (not pd_body);
  1097. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1098. pdflags:=pdflags or pd_global;
  1099. { Add the correct flag }
  1100. aktprocsym^.definition^.proccalloptions:=aktprocsym^.definition^.proccalloptions+proc_direcdata[p].pocall;
  1101. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+proc_direcdata[p].pooption;
  1102. { Adjust positions of args for cdecl or stdcall }
  1103. if (aktprocsym^.definition^.deftype=procdef) and
  1104. (([pocall_cdecl,pocall_stdcall]*aktprocsym^.definition^.proccalloptions)<>[]) then
  1105. aktprocsym^.definition^.parast^.set_alignment(target_os.size_of_longint);
  1106. { Call the handler }
  1107. if pointer({$ifndef FPC}@{$endif}proc_direcdata[p].handler)<>nil then
  1108. proc_direcdata[p].handler(proc_names);
  1109. end;
  1110. {***************************************************************************}
  1111. function check_identical(var p : pprocdef) : boolean;
  1112. {
  1113. Search for idendical definitions,
  1114. if there is a forward, then kill this.
  1115. Returns the result of the forward check.
  1116. Removed from unter_dec to keep the source readable
  1117. }
  1118. var
  1119. hd,pd : Pprocdef;
  1120. storeparast : psymtable;
  1121. ad,fd : psym;
  1122. s : string;
  1123. begin
  1124. check_identical:=false;
  1125. p:=nil;
  1126. pd:=aktprocsym^.definition;
  1127. if assigned(pd) then
  1128. begin
  1129. { Is there an overload/forward ? }
  1130. if assigned(pd^.nextoverloaded) then
  1131. begin
  1132. { walk the procdef list }
  1133. while (assigned(pd)) and (assigned(pd^.nextoverloaded)) do
  1134. begin
  1135. if (not(m_repeat_forward in aktmodeswitches) and
  1136. (aktprocsym^.definition^.para^.count=0)) or
  1137. (equal_paras(aktprocsym^.definition^.para,pd^.nextoverloaded^.para,false) and
  1138. { for operators equal_paras is not enough !! }
  1139. ((aktprocsym^.definition^.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1140. is_equal(pd^.nextoverloaded^.rettype.def,aktprocsym^.definition^.rettype.def))) then
  1141. begin
  1142. if pd^.nextoverloaded^.forwarddef then
  1143. { remove the forward definition but don't delete it, }
  1144. { the symtable is the owner !! }
  1145. begin
  1146. hd:=pd^.nextoverloaded;
  1147. { Check if the procedure type and return type are correct }
  1148. if (hd^.proctypeoption<>aktprocsym^.definition^.proctypeoption) or
  1149. (not(is_equal(hd^.rettype.def,aktprocsym^.definition^.rettype.def)) and
  1150. (m_repeat_forward in aktmodeswitches)) then
  1151. begin
  1152. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1153. exit;
  1154. end;
  1155. { Check calling convention, no check for internconst,internproc which
  1156. are only defined in interface or implementation }
  1157. if (hd^.proccalloptions-[pocall_internconst,pocall_internproc]<>
  1158. aktprocsym^.definition^.proccalloptions-[pocall_internconst,pocall_internproc]) then
  1159. begin
  1160. { only trigger an error, becuase it doesn't hurt }
  1161. Message(parser_e_call_convention_dont_match_forward);
  1162. { set the mangledname to the interface name so it doesn't trigger
  1163. the Note about different manglednames (PFV) }
  1164. aktprocsym^.definition^.setmangledname(hd^.mangledname);
  1165. end;
  1166. { manglednames are equal? }
  1167. hd^.count:=false;
  1168. if (m_repeat_forward in aktmodeswitches) or
  1169. aktprocsym^.definition^.haspara then
  1170. begin
  1171. if (hd^.mangledname<>aktprocsym^.definition^.mangledname) then
  1172. begin
  1173. { When overloading is not possible then we issue an error }
  1174. { This is not true, tp7/delphi don't give an error when a renamed
  1175. type is used in the other declaration (PFV)
  1176. if not(m_repeat_forward in aktmodeswitches) then
  1177. begin
  1178. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1179. exit;
  1180. end; }
  1181. if not(po_external in aktprocsym^.definition^.procoptions) then
  1182. Message2(parser_n_interface_name_diff_implementation_name,hd^.mangledname,
  1183. aktprocsym^.definition^.mangledname);
  1184. { reset the mangledname of the interface part to be sure }
  1185. { this is wrong because the mangled name might have been used already !! }
  1186. if hd^.is_used then
  1187. renameasmsymbol(hd^.mangledname,aktprocsym^.definition^.mangledname);
  1188. hd^.setmangledname(aktprocsym^.definition^.mangledname);
  1189. { so we need to keep the name of interface !!
  1190. No!!!! The procedure directives can change the mangledname.
  1191. I fixed this by first calling check_identical and then doing
  1192. the proc directives, but this is not a good solution.(DM)}
  1193. { this is also wrong (PM)
  1194. aktprocsym^.definition^.setmangledname(hd^.mangledname);}
  1195. end
  1196. else
  1197. begin
  1198. { If mangled names are equal, therefore }
  1199. { they have the same number of parameters }
  1200. { Therefore we can check the name of these }
  1201. { parameters... }
  1202. if hd^.forwarddef and aktprocsym^.definition^.forwarddef then
  1203. begin
  1204. Message1(parser_e_function_already_declared_public_forward,aktprocsym^.demangledName);
  1205. Check_identical:=true;
  1206. { Remove other forward from the list to reduce errors }
  1207. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1208. exit;
  1209. end;
  1210. ad:=psym(hd^.parast^.symindex^.first);
  1211. fd:=psym(aktprocsym^.definition^.parast^.symindex^.first);
  1212. if assigned(ad) and assigned(fd) then
  1213. begin
  1214. while assigned(ad) and assigned(fd) do
  1215. begin
  1216. s:=ad^.name;
  1217. if s<>fd^.name then
  1218. begin
  1219. Message3(parser_e_header_different_var_names,
  1220. aktprocsym^.name,s,fd^.name);
  1221. break;
  1222. end;
  1223. { it is impossible to have a nil pointer }
  1224. { for only one parameter - since they }
  1225. { have the same number of parameters. }
  1226. { Left = next parameter. }
  1227. ad:=psym(ad^.left);
  1228. fd:=psym(fd^.left);
  1229. end;
  1230. end;
  1231. end;
  1232. end;
  1233. { also the para_offset }
  1234. hd^.parast^.address_fixup:=aktprocsym^.definition^.parast^.address_fixup;
  1235. hd^.count:=true;
  1236. { remove pd^.nextoverloaded from the list }
  1237. { and add aktprocsym^.definition }
  1238. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1239. hd^.nextoverloaded:=aktprocsym^.definition^.nextoverloaded;
  1240. { Alert! All fields of aktprocsym^.definition that are modified
  1241. by the procdir handlers must be copied here!.}
  1242. hd^.forwarddef:=false;
  1243. hd^.proccalloptions:=hd^.proccalloptions + aktprocsym^.definition^.proccalloptions;
  1244. hd^.procoptions:=hd^.procoptions + aktprocsym^.definition^.procoptions;
  1245. if aktprocsym^.definition^.extnumber=-1 then
  1246. aktprocsym^.definition^.extnumber:=hd^.extnumber
  1247. else
  1248. if hd^.extnumber=-1 then
  1249. hd^.extnumber:=aktprocsym^.definition^.extnumber;
  1250. { switch parast for warning in implementation PM }
  1251. if (m_repeat_forward in aktmodeswitches) or
  1252. aktprocsym^.definition^.haspara then
  1253. begin
  1254. storeparast:=hd^.parast;
  1255. hd^.parast:=aktprocsym^.definition^.parast;
  1256. aktprocsym^.definition^.parast:=storeparast;
  1257. end;
  1258. if pd=aktprocsym^.definition then
  1259. p:=nil
  1260. else
  1261. p:=pd;
  1262. aktprocsym^.definition:=hd;
  1263. check_identical:=true;
  1264. end
  1265. else
  1266. { abstract methods aren't forward defined, but this }
  1267. { needs another error message }
  1268. if not(po_abstractmethod in pd^.nextoverloaded^.procoptions) then
  1269. Message(parser_e_overloaded_have_same_parameters)
  1270. else
  1271. Message(parser_e_abstract_no_definition);
  1272. break;
  1273. end;
  1274. pd:=pd^.nextoverloaded;
  1275. end;
  1276. end
  1277. else
  1278. begin
  1279. { there is no overloaded, so its always identical with itself }
  1280. check_identical:=true;
  1281. end;
  1282. end;
  1283. { insert opsym only in the right symtable }
  1284. if ((procinfo^.flags and pi_operator)<>0) and assigned(opsym)
  1285. and not parse_only then
  1286. begin
  1287. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  1288. begin
  1289. pprocdef(aktprocsym^.definition)^.parast^.insert(opsym);
  1290. { this increases the data size }
  1291. { correct this to get the right ret $value }
  1292. dec(pprocdef(aktprocsym^.definition)^.parast^.datasize,opsym^.getpushsize);
  1293. { this allows to read the funcretoffset }
  1294. opsym^.address:=-4;
  1295. opsym^.varspez:=vs_var;
  1296. end
  1297. else
  1298. pprocdef(aktprocsym^.definition)^.localst^.insert(opsym);
  1299. end;
  1300. end;
  1301. procedure compile_proc_body(const proc_names:Tstringcontainer;
  1302. make_global,parent_has_class:boolean);
  1303. {
  1304. Compile the body of a procedure
  1305. }
  1306. var
  1307. oldexitlabel,oldexit2label : pasmlabel;
  1308. oldfaillabel,oldquickexitlabel:Pasmlabel;
  1309. _class,hp:Pobjectdef;
  1310. { switches can change inside the procedure }
  1311. entryswitches, exitswitches : tlocalswitches;
  1312. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  1313. { code for the subroutine as tree }
  1314. {$ifdef newcg}
  1315. code:ptree;
  1316. {$else newcg}
  1317. code:ptree;
  1318. {$endif newcg}
  1319. { size of the local strackframe }
  1320. stackframe:longint;
  1321. { true when no stackframe is required }
  1322. nostackframe:boolean;
  1323. { number of bytes which have to be cleared by RET }
  1324. parasize:longint;
  1325. { filepositions }
  1326. entrypos,
  1327. savepos,
  1328. exitpos : tfileposinfo;
  1329. begin
  1330. { calculate the lexical level }
  1331. inc(lexlevel);
  1332. if lexlevel>32 then
  1333. Message(parser_e_too_much_lexlevel);
  1334. { static is also important for local procedures !! }
  1335. if (po_staticmethod in aktprocsym^.definition^.procoptions) then
  1336. allow_only_static:=true
  1337. else if (lexlevel=normal_function_level) then
  1338. allow_only_static:=false;
  1339. { save old labels }
  1340. oldexitlabel:=aktexitlabel;
  1341. oldexit2label:=aktexit2label;
  1342. oldquickexitlabel:=quickexitlabel;
  1343. oldfaillabel:=faillabel;
  1344. { get new labels }
  1345. getlabel(aktexitlabel);
  1346. getlabel(aktexit2label);
  1347. { exit for fail in constructors }
  1348. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1349. begin
  1350. getlabel(faillabel);
  1351. getlabel(quickexitlabel);
  1352. end;
  1353. { reset break and continue labels }
  1354. block_type:=bt_general;
  1355. aktbreaklabel:=nil;
  1356. aktcontinuelabel:=nil;
  1357. { insert symtables for the class, by only if it is no nested function }
  1358. if assigned(procinfo^._class) and not(parent_has_class) then
  1359. begin
  1360. { insert them in the reverse order ! }
  1361. hp:=nil;
  1362. repeat
  1363. _class:=procinfo^._class;
  1364. while _class^.childof<>hp do
  1365. _class:=_class^.childof;
  1366. hp:=_class;
  1367. _class^.symtable^.next:=symtablestack;
  1368. symtablestack:=_class^.symtable;
  1369. until hp=procinfo^._class;
  1370. end;
  1371. { insert parasymtable in symtablestack}
  1372. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  1373. for checking of same names used in interface and implementation !! }
  1374. if lexlevel>=normal_function_level then
  1375. begin
  1376. aktprocsym^.definition^.parast^.next:=symtablestack;
  1377. symtablestack:=aktprocsym^.definition^.parast;
  1378. symtablestack^.symtablelevel:=lexlevel;
  1379. end;
  1380. { insert localsymtable in symtablestack}
  1381. aktprocsym^.definition^.localst^.next:=symtablestack;
  1382. symtablestack:=aktprocsym^.definition^.localst;
  1383. symtablestack^.symtablelevel:=lexlevel;
  1384. { constant symbols are inserted in this symboltable }
  1385. constsymtable:=symtablestack;
  1386. { reset the temporary memory }
  1387. cleartempgen;
  1388. {$ifdef newcg}
  1389. tg.usedinproc:=[];
  1390. {$else newcg}
  1391. { no registers are used }
  1392. usedinproc:=0;
  1393. {$endif newcg}
  1394. { save entry info }
  1395. entrypos:=aktfilepos;
  1396. entryswitches:=aktlocalswitches;
  1397. localmaxfpuregisters:=aktmaxfpuregisters;
  1398. {$ifdef newcg}
  1399. {$ifdef dummy}
  1400. { parse the code ... }
  1401. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1402. code:=convtree2node(assembler_block)
  1403. else
  1404. code:=convtree2node(block(current_module^.islibrary));
  1405. {$endif dummy}
  1406. { parse the code ... }
  1407. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1408. code:=assembler_block
  1409. else
  1410. code:=block(current_module^.islibrary);
  1411. {$else newcg}
  1412. { parse the code ... }
  1413. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1414. code:=assembler_block
  1415. else
  1416. code:=block(current_module^.islibrary);
  1417. {$endif newcg}
  1418. { get a better entry point }
  1419. if assigned(code) then
  1420. entrypos:=code^.fileinfo;
  1421. { save exit info }
  1422. exitswitches:=aktlocalswitches;
  1423. exitpos:=last_endtoken_filepos;
  1424. { save current filepos }
  1425. savepos:=aktfilepos;
  1426. {When we are called to compile the body of a unit, aktprocsym should
  1427. point to the unit initialization. If the unit has no initialization,
  1428. aktprocsym=nil. But in that case code=nil. hus we should check for
  1429. code=nil, when we use aktprocsym.}
  1430. { set the framepointer to esp for assembler functions }
  1431. { but only if the are no local variables }
  1432. { already done in assembler_block }
  1433. {$ifdef newcg}
  1434. tg.setfirsttemp(procinfo^.firsttemp_offset);
  1435. {$else newcg}
  1436. setfirsttemp(procinfo^.firsttemp_offset);
  1437. {$endif newcg}
  1438. { ... and generate assembler }
  1439. { but set the right switches for entry !! }
  1440. aktlocalswitches:=entryswitches;
  1441. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  1442. aktmaxfpuregisters:=localmaxfpuregisters;
  1443. {$ifndef NOPASS2}
  1444. {$ifdef newcg}
  1445. if assigned(code) then
  1446. generatecode(code);
  1447. {$else newcg}
  1448. if assigned(code) then
  1449. generatecode(code);
  1450. {$endif newcg}
  1451. { set switches to status at end of procedure }
  1452. aktlocalswitches:=exitswitches;
  1453. if assigned(code) then
  1454. begin
  1455. aktprocsym^.definition^.code:=code;
  1456. { the procedure is now defined }
  1457. aktprocsym^.definition^.forwarddef:=false;
  1458. {$ifdef newcg}
  1459. aktprocsym^.definition^.usedregisters:=tg.usedinproc;
  1460. {$else newcg}
  1461. aktprocsym^.definition^.usedregisters:=usedinproc;
  1462. {$endif newcg}
  1463. end;
  1464. {$ifdef newcg}
  1465. stackframe:=tg.gettempsize;
  1466. {$else newcg}
  1467. stackframe:=gettempsize;
  1468. {$endif newcg}
  1469. { first generate entry code with the correct position and switches }
  1470. aktfilepos:=entrypos;
  1471. aktlocalswitches:=entryswitches;
  1472. {$ifdef newcg}
  1473. if assigned(code) then
  1474. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1475. {$else newcg}
  1476. if assigned(code) then
  1477. genentrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1478. {$endif newcg}
  1479. { now generate exit code with the correct position and switches }
  1480. aktfilepos:=exitpos;
  1481. aktlocalswitches:=exitswitches;
  1482. if assigned(code) then
  1483. begin
  1484. {$ifdef newcg}
  1485. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1486. {$else newcg}
  1487. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1488. {$endif newcg}
  1489. procinfo^.aktproccode^.insertlist(procinfo^.aktentrycode);
  1490. procinfo^.aktproccode^.concatlist(procinfo^.aktexitcode);
  1491. {$ifdef i386}
  1492. {$ifndef NoOpt}
  1493. if (cs_optimize in aktglobalswitches) and
  1494. { do not optimize pure assembler procedures }
  1495. ((procinfo^.flags and pi_is_assembler)=0) then
  1496. Optimize(procinfo^.aktproccode);
  1497. {$endif NoOpt}
  1498. {$endif}
  1499. { save local data (casetable) also in the same file }
  1500. if assigned(procinfo^.aktlocaldata) and
  1501. (not procinfo^.aktlocaldata^.empty) then
  1502. begin
  1503. procinfo^.aktproccode^.concat(new(pai_section,init(sec_data)));
  1504. procinfo^.aktproccode^.concatlist(procinfo^.aktlocaldata);
  1505. procinfo^.aktproccode^.concat(new(pai_section,init(sec_code)));
  1506. end;
  1507. { now we can insert a cut }
  1508. if (cs_create_smart in aktmoduleswitches) then
  1509. codesegment^.concat(new(pai_cut,init));
  1510. { add the procedure to the codesegment }
  1511. codesegment^.concatlist(procinfo^.aktproccode);
  1512. end;
  1513. {$else}
  1514. if assigned(code) then
  1515. firstpass(code);
  1516. {$endif NOPASS2}
  1517. { ... remove symbol tables, for the browser leave the static table }
  1518. { if (cs_browser in aktmoduleswitches) and (symtablestack^.symtabletype=staticsymtable) then
  1519. symtablestack^.next:=symtablestack^.next^.next
  1520. else }
  1521. if lexlevel>=normal_function_level then
  1522. symtablestack:=symtablestack^.next^.next
  1523. else
  1524. symtablestack:=symtablestack^.next;
  1525. { ... check for unused symbols }
  1526. { but only if there is no asm block }
  1527. if assigned(code) then
  1528. begin
  1529. if (Errorcount=0) then
  1530. begin
  1531. aktprocsym^.definition^.localst^.check_forwards;
  1532. aktprocsym^.definition^.localst^.checklabels;
  1533. end;
  1534. if (procinfo^.flags and pi_uses_asm)=0 then
  1535. begin
  1536. { not for unit init, becuase the var can be used in finalize,
  1537. it will be done in proc_unit }
  1538. if not(aktprocsym^.definition^.proctypeoption
  1539. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  1540. aktprocsym^.definition^.localst^.allsymbolsused;
  1541. aktprocsym^.definition^.parast^.allsymbolsused;
  1542. end;
  1543. end;
  1544. { the local symtables can be deleted, but the parast }
  1545. { doesn't, (checking definitons when calling a }
  1546. { function }
  1547. { not for a inline procedure !! (PM) }
  1548. { at lexlevel = 1 localst is the staticsymtable itself }
  1549. { so no dispose here !! }
  1550. if assigned(code) and
  1551. not(cs_browser in aktmoduleswitches) and
  1552. not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1553. begin
  1554. if lexlevel>=normal_function_level then
  1555. dispose(aktprocsym^.definition^.localst,done);
  1556. aktprocsym^.definition^.localst:=nil;
  1557. end;
  1558. {$ifdef newcg}
  1559. { all registers can be used again }
  1560. tg.resetusableregisters;
  1561. { only now we can remove the temps }
  1562. tg.resettempgen;
  1563. {$else newcg}
  1564. { all registers can be used again }
  1565. resetusableregisters;
  1566. { only now we can remove the temps }
  1567. resettempgen;
  1568. {$endif newcg}
  1569. { remove code tree, if not inline procedure }
  1570. if assigned(code) and not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1571. {$ifdef newcg}
  1572. {!!!!!!! dispose(code,done); }
  1573. disposetree(code);
  1574. {$else newcg}
  1575. disposetree(code);
  1576. {$endif newcg}
  1577. { remove class member symbol tables }
  1578. while symtablestack^.symtabletype=objectsymtable do
  1579. symtablestack:=symtablestack^.next;
  1580. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  1581. { restore filepos, the switches are already set }
  1582. aktfilepos:=savepos;
  1583. { restore labels }
  1584. aktexitlabel:=oldexitlabel;
  1585. aktexit2label:=oldexit2label;
  1586. quickexitlabel:=oldquickexitlabel;
  1587. faillabel:=oldfaillabel;
  1588. { reset to normal non static function }
  1589. if (lexlevel=normal_function_level) then
  1590. allow_only_static:=false;
  1591. { previous lexlevel }
  1592. dec(lexlevel);
  1593. end;
  1594. procedure parse_proc_directives(Anames:Pstringcontainer;var pdflags:word);
  1595. {
  1596. Parse the procedure directives. It does not matter if procedure directives
  1597. are written using ;procdir; or ['procdir'] syntax.
  1598. }
  1599. var
  1600. res : boolean;
  1601. begin
  1602. while token in [_ID,_LECKKLAMMER] do
  1603. begin
  1604. if try_to_consume(_LECKKLAMMER) then
  1605. begin
  1606. repeat
  1607. parse_proc_direc(Anames^,pdflags);
  1608. until not try_to_consume(_COMMA);
  1609. consume(_RECKKLAMMER);
  1610. { we always expect at least '[];' }
  1611. res:=true;
  1612. end
  1613. else
  1614. res:=parse_proc_direc(Anames^,pdflags);
  1615. { A procedure directive normally followed by a semicolon, but in
  1616. a const section we should stop when _EQUAL is found }
  1617. if res then
  1618. begin
  1619. if (block_type=bt_const) and
  1620. (token=_EQUAL) then
  1621. break;
  1622. consume(_SEMICOLON);
  1623. end
  1624. else
  1625. break;
  1626. end;
  1627. end;
  1628. procedure parse_var_proc_directives(var sym : psym);
  1629. var
  1630. anames : pstringcontainer;
  1631. pdflags : word;
  1632. oldsym : pprocsym;
  1633. pd : pabstractprocdef;
  1634. begin
  1635. oldsym:=aktprocsym;
  1636. anames:=new(pstringcontainer,init);
  1637. pdflags:=pd_procvar;
  1638. { we create a temporary aktprocsym to read the directives }
  1639. aktprocsym:=new(pprocsym,init(sym^.name));
  1640. case sym^.typ of
  1641. varsym :
  1642. pd:=pabstractprocdef(pvarsym(sym)^.vartype.def);
  1643. typedconstsym :
  1644. pd:=pabstractprocdef(ptypedconstsym(sym)^.typedconsttype.def);
  1645. typesym :
  1646. pd:=pabstractprocdef(ptypesym(sym)^.restype.def);
  1647. else
  1648. internalerror(994932432);
  1649. end;
  1650. if pd^.deftype<>procvardef then
  1651. internalerror(994932433);
  1652. pabstractprocdef(aktprocsym^.definition):=pd;
  1653. { names should never be used anyway }
  1654. inc(lexlevel);
  1655. parse_proc_directives(anames,pdflags);
  1656. dec(lexlevel);
  1657. aktprocsym^.definition:=nil;
  1658. dispose(aktprocsym,done);
  1659. dispose(anames,done);
  1660. aktprocsym:=oldsym;
  1661. end;
  1662. procedure parse_object_proc_directives(var sym : pprocsym);
  1663. var
  1664. anames : pstringcontainer;
  1665. pdflags : word;
  1666. begin
  1667. pdflags:=pd_object;
  1668. anames:=new(pstringcontainer,init);
  1669. inc(lexlevel);
  1670. parse_proc_directives(anames,pdflags);
  1671. dec(lexlevel);
  1672. dispose(anames,done);
  1673. if (po_containsself in aktprocsym^.definition^.procoptions) and
  1674. (([po_msgstr,po_msgint]*aktprocsym^.definition^.procoptions)=[]) then
  1675. Message(parser_e_self_in_non_message_handler);
  1676. end;
  1677. procedure checkvaluepara(p:pnamedindexobject);{$ifndef FPC}far;{$endif}
  1678. var
  1679. vs : pvarsym;
  1680. s : string;
  1681. begin
  1682. with pvarsym(p)^ do
  1683. begin
  1684. if copy(name,1,3)='val' then
  1685. begin
  1686. s:=Copy(name,4,255);
  1687. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  1688. begin
  1689. vs:=new(Pvarsym,initdef(s,vartype.def));
  1690. vs^.fileinfo:=fileinfo;
  1691. vs^.varspez:=varspez;
  1692. aktprocsym^.definition^.localst^.insert(vs);
  1693. {$ifdef INCLUDEOK}
  1694. include(vs^.varoptions,vo_is_local_copy);
  1695. {$else}
  1696. vs^.varoptions:=vs^.varoptions+[vo_is_local_copy];
  1697. {$endif}
  1698. vs^.varstate:=vs_assigned;
  1699. localvarsym:=vs;
  1700. inc(refs); { the para was used to set the local copy ! }
  1701. { warnings only on local copy ! }
  1702. varstate:=vs_used;
  1703. end
  1704. else
  1705. begin
  1706. aktprocsym^.definition^.parast^.rename(name,s);
  1707. end;
  1708. end;
  1709. end;
  1710. end;
  1711. procedure read_proc;
  1712. {
  1713. Parses the procedure directives, then parses the procedure body, then
  1714. generates the code for it
  1715. }
  1716. var
  1717. oldprefix : string;
  1718. oldprocsym : Pprocsym;
  1719. oldprocinfo : pprocinfo;
  1720. oldconstsymtable : Psymtable;
  1721. oldfilepos : tfileposinfo;
  1722. names : Pstringcontainer;
  1723. pdflags : word;
  1724. prevdef,stdef : pprocdef;
  1725. begin
  1726. { save old state }
  1727. oldprocsym:=aktprocsym;
  1728. oldprefix:=procprefix;
  1729. oldconstsymtable:=constsymtable;
  1730. oldprocinfo:=procinfo;
  1731. { create a new procedure }
  1732. new(names,init);
  1733. {$ifdef fixLeaksOnError}
  1734. strContStack.push(names);
  1735. {$endif fixLeaksOnError}
  1736. codegen_newprocedure;
  1737. with procinfo^ do
  1738. begin
  1739. parent:=oldprocinfo;
  1740. { clear flags }
  1741. flags:=0;
  1742. { standard frame pointer }
  1743. framepointer:=frame_pointer;
  1744. { funcret_is_valid:=false; }
  1745. funcret_state:=vs_declared;
  1746. { is this a nested function of a method ? }
  1747. if assigned(oldprocinfo) then
  1748. _class:=oldprocinfo^._class;
  1749. end;
  1750. parse_proc_dec;
  1751. procinfo^.sym:=aktprocsym;
  1752. procinfo^.def:=aktprocsym^.definition;
  1753. { set the default function options }
  1754. if parse_only then
  1755. begin
  1756. aktprocsym^.definition^.forwarddef:=true;
  1757. { set also the interface flag, for better error message when the
  1758. implementation doesn't much this header }
  1759. aktprocsym^.definition^.interfacedef:=true;
  1760. pdflags:=pd_interface;
  1761. end
  1762. else
  1763. begin
  1764. pdflags:=pd_body;
  1765. if current_module^.in_implementation then
  1766. pdflags:=pdflags or pd_implemen;
  1767. if (not current_module^.is_unit) or (cs_create_smart in aktmoduleswitches) then
  1768. pdflags:=pdflags or pd_global;
  1769. procinfo^.exported:=false;
  1770. aktprocsym^.definition^.forwarddef:=false;
  1771. end;
  1772. { parse the directives that may follow }
  1773. inc(lexlevel);
  1774. parse_proc_directives(names,pdflags);
  1775. dec(lexlevel);
  1776. { set aktfilepos to the beginning of the function declaration }
  1777. oldfilepos:=aktfilepos;
  1778. aktfilepos:=aktprocsym^.definition^.fileinfo;
  1779. { search for forward declarations }
  1780. if not check_identical(prevdef) then
  1781. begin
  1782. { A method must be forward defined (in the object declaration) }
  1783. if assigned(procinfo^._class) and (not assigned(oldprocinfo^._class)) then
  1784. Message(parser_e_header_dont_match_any_member);
  1785. { Give a better error if there is a forward def in the interface and only
  1786. a single implementation }
  1787. if (not aktprocsym^.definition^.forwarddef) and
  1788. assigned(aktprocsym^.definition^.nextoverloaded) and
  1789. aktprocsym^.definition^.nextoverloaded^.forwarddef and
  1790. aktprocsym^.definition^.nextoverloaded^.interfacedef and
  1791. not(assigned(aktprocsym^.definition^.nextoverloaded^.nextoverloaded)) then
  1792. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName)
  1793. else
  1794. begin
  1795. { check the global flag }
  1796. if (procinfo^.flags and pi_is_global)<>0 then
  1797. Message(parser_e_overloaded_must_be_all_global);
  1798. end
  1799. end;
  1800. { set return type here, becuase the aktprocsym^.definition can be
  1801. changed by check_identical (PFV) }
  1802. procinfo^.returntype.def:=aktprocsym^.definition^.rettype.def;
  1803. { pointer to the return value ? }
  1804. if ret_in_param(procinfo^.returntype.def) then
  1805. begin
  1806. procinfo^.return_offset:=procinfo^.para_offset;
  1807. inc(procinfo^.para_offset,target_os.size_of_pointer);
  1808. end;
  1809. { allows to access the parameters of main functions in nested functions }
  1810. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.para_offset;
  1811. { when it is a value para and it needs a local copy then rename
  1812. the parameter and insert a copy in the localst. This is not done
  1813. for assembler procedures }
  1814. if (not parse_only) and (not aktprocsym^.definition^.forwarddef) then
  1815. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}checkvaluepara);
  1816. { restore file pos }
  1817. aktfilepos:=oldfilepos;
  1818. { compile procedure when a body is needed }
  1819. if (pdflags and pd_body)<>0 then
  1820. begin
  1821. Message1(parser_p_procedure_start,aktprocsym^.demangledname);
  1822. names^.insert(aktprocsym^.definition^.mangledname);
  1823. { set _FAIL as keyword if constructor }
  1824. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1825. tokeninfo^[_FAIL].keyword:=m_all;
  1826. if assigned(aktprocsym^.definition^._class) then
  1827. tokeninfo^[_SELF].keyword:=m_all;
  1828. compile_proc_body(names^,((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  1829. { reset _FAIL as normal }
  1830. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1831. tokeninfo^[_FAIL].keyword:=m_none;
  1832. if assigned(aktprocsym^.definition^._class) and (lexlevel=main_program_level) then
  1833. tokeninfo^[_SELF].keyword:=m_none;
  1834. consume(_SEMICOLON);
  1835. end;
  1836. { close }
  1837. {$ifdef fixLeaksOnError}
  1838. if names <> strContStack.pop then
  1839. writeln('problem with strContStack in psub!');
  1840. {$endif fixLeaksOnError}
  1841. dispose(names,done);
  1842. codegen_doneprocedure;
  1843. { Restore old state }
  1844. constsymtable:=oldconstsymtable;
  1845. { from now on all refernece to mangledname means
  1846. that the function is already used }
  1847. aktprocsym^.definition^.count:=true;
  1848. { restore the interface order to maintain CRC values PM }
  1849. if assigned(prevdef) and assigned(aktprocsym^.definition^.nextoverloaded) then
  1850. begin
  1851. stdef:=aktprocsym^.definition;
  1852. aktprocsym^.definition:=stdef^.nextoverloaded;
  1853. stdef^.nextoverloaded:=prevdef^.nextoverloaded;
  1854. prevdef^.nextoverloaded:=stdef;
  1855. end;
  1856. aktprocsym:=oldprocsym;
  1857. procprefix:=oldprefix;
  1858. procinfo:=oldprocinfo;
  1859. opsym:=nil;
  1860. end;
  1861. end.
  1862. {
  1863. $Log$
  1864. Revision 1.55 2000-03-27 11:57:22 pierre
  1865. * fix for bug 890
  1866. Revision 1.54 2000/03/23 22:17:51 pierre
  1867. * fix tf000008 bug
  1868. Revision 1.53 2000/03/16 16:41:13 pierre
  1869. * fix for bug 807
  1870. Revision 1.52 2000/03/15 23:10:00 pierre
  1871. * fix for bug 848 (that still genrated wrong code)
  1872. + better testing for variables used in assembler
  1873. (gives an error if variable is not directly reachable !)
  1874. Revision 1.51 2000/02/27 14:44:39 peter
  1875. * if calling convention doesn't match don't print note about
  1876. different manglednames
  1877. Revision 1.50 2000/02/20 20:49:45 florian
  1878. * newcg is compiling
  1879. * fixed the dup id problem reported by Paul Y.
  1880. Revision 1.49 2000/02/17 14:53:42 florian
  1881. * some updates for the newcg
  1882. Revision 1.48 2000/02/09 13:23:00 peter
  1883. * log truncated
  1884. Revision 1.47 2000/02/08 13:55:13 peter
  1885. * reset section back to code after localdata
  1886. Revision 1.46 2000/02/04 20:00:22 florian
  1887. * an exception in a construcor calls now the destructor (this applies only
  1888. to classes)
  1889. Revision 1.45 2000/02/04 14:54:17 jonas
  1890. * moved call to resetusableregs to compile_proc_body (put it right before the
  1891. reset of the temp generator) so the optimizer can know which registers are
  1892. regvars
  1893. Revision 1.44 2000/01/28 23:17:53 florian
  1894. * virtual XXXX; support for objects, only if -dWITHDMT is defined
  1895. Revision 1.43 2000/01/21 22:06:16 florian
  1896. * fixed for the fix of bug 793
  1897. * fpu variables modified by nested subroutines aren't regable anymore
  1898. * $maxfpuregisters doesn't modify anymore the behavior of a procedure before
  1899. Revision 1.42 2000/01/16 22:17:12 peter
  1900. * renamed call_offset to para_offset
  1901. Revision 1.41 2000/01/11 17:16:06 jonas
  1902. * removed a lot of memory leaks when an error is encountered (caused by
  1903. procinfo and pstringcontainers). There are still plenty left though :)
  1904. Revision 1.40 2000/01/07 01:14:31 peter
  1905. * updated copyright to 2000
  1906. Revision 1.39 1999/12/22 01:01:52 peter
  1907. - removed freelabel()
  1908. * added undefined label detection in internal assembler, this prevents
  1909. a lot of ld crashes and wrong .o files
  1910. * .o files aren't written anymore if errors have occured
  1911. * inlining of assembler labels is now correct
  1912. Revision 1.38 1999/12/06 18:17:09 peter
  1913. * newcg compiler compiles again
  1914. Revision 1.37 1999/11/30 10:40:48 peter
  1915. + ttype, tsymlist
  1916. Revision 1.36 1999/11/22 00:23:09 pierre
  1917. * also complain about unused functions in program
  1918. Revision 1.35 1999/11/17 17:05:02 pierre
  1919. * Notes/hints changes
  1920. Revision 1.34 1999/11/10 00:24:02 pierre
  1921. * more browser details
  1922. Revision 1.33 1999/11/09 23:43:08 pierre
  1923. * better browser info
  1924. Revision 1.32 1999/11/09 23:06:45 peter
  1925. * esi_offset -> selfpointer_offset to be newcg compatible
  1926. * hcogegen -> cgbase fixes for newcg
  1927. Revision 1.31 1999/11/06 14:34:23 peter
  1928. * truncated log to 20 revs
  1929. Revision 1.30 1999/10/26 12:30:44 peter
  1930. * const parameter is now checked
  1931. * better and generic check if a node can be used for assigning
  1932. * export fixes
  1933. * procvar equal works now (it never had worked at least from 0.99.8)
  1934. * defcoll changed to linkedlist with pparaitem so it can easily be
  1935. walked both directions
  1936. Revision 1.29 1999/10/22 10:39:35 peter
  1937. * split type reading from pdecl to ptype unit
  1938. * parameter_dec routine is now used for procedure and procvars
  1939. Revision 1.28 1999/10/13 10:37:36 peter
  1940. * moved mangledname creation of normal proc so it also handles a wrong
  1941. method proc
  1942. }