pdecsub.pas 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 pdecsub;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,symconst,symtype,symdef,symsym;
  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. pd_notobjintf= $80; { directive can not be used interface declaration }
  32. function is_proc_directive(tok:ttoken):boolean;
  33. procedure parameter_dec(aktprocdef:tabstractprocdef);
  34. procedure parse_proc_directives(var pdflags:word);
  35. procedure handle_calling_convention(sym:tprocsym;def:tabstractprocdef);
  36. procedure calc_parasymtable_addresses(def:tprocdef);
  37. procedure parse_proc_head(options:tproctypeoption);
  38. procedure parse_proc_dec;
  39. procedure parse_var_proc_directives(var sym : tsym);
  40. procedure parse_object_proc_directives(var sym : tprocsym);
  41. function proc_add_definition(aprocsym:tprocsym;var aprocdef : tprocdef) : boolean;
  42. implementation
  43. uses
  44. {$ifdef delphi}
  45. sysutils,
  46. {$else delphi}
  47. strings,
  48. {$endif delphi}
  49. { common }
  50. cutils,cclasses,
  51. { global }
  52. globtype,globals,verbose,
  53. systems,cpubase,
  54. { aasm }
  55. aasmbase,aasmtai,aasmcpu,
  56. { symtable }
  57. symbase,symtable,defutil,defcmp,paramgr,
  58. { pass 1 }
  59. node,htypechk,
  60. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  61. { parser }
  62. fmodule,scanner,
  63. pbase,pexpr,ptype,pdecl,
  64. { linking }
  65. import,gendef,
  66. { codegen }
  67. cpuinfo,cgbase
  68. ;
  69. procedure checkvaluepara(p:tnamedindexitem;arg:pointer);
  70. begin
  71. if tsym(p).typ<>varsym then
  72. exit;
  73. with tvarsym(p) do
  74. begin
  75. { do we need a local copy? Then rename the varsym, do this after the
  76. insert so the dup id checking is done correctly.
  77. array of const and open array do not need this, the local copy routine
  78. will patch the pushed value to point to the local copy }
  79. if (varspez=vs_value) and
  80. paramanager.push_addr_param(vartype.def,aktprocdef.proccalloption) and
  81. not(is_array_of_const(vartype.def) or
  82. is_open_array(vartype.def)) then
  83. aktprocdef.parast.symsearch.rename(name,'val'+name);
  84. end;
  85. end;
  86. procedure checkparatype(p:tnamedindexitem;arg:pointer);
  87. var
  88. highname : string;
  89. begin
  90. if tsym(p).typ<>varsym then
  91. exit;
  92. with tvarsym(p) do
  93. begin
  94. if assigned(vartype.def) and
  95. (vartype.def.deftype=arraydef) and
  96. {not is_array_constructor(vartype.def) and}
  97. not is_variant_array(vartype.def) and
  98. not is_array_of_const(vartype.def) then
  99. begin
  100. if (varspez<>vs_var) then
  101. Message(parser_h_c_arrays_are_references);
  102. varspez:=vs_var;
  103. end;
  104. if assigned(vartype.def) and
  105. (is_array_of_const(vartype.def) or
  106. is_open_array(vartype.def) or
  107. is_shortstring(vartype.def)) then
  108. begin
  109. if is_open_string(vartype.def) then
  110. begin
  111. { change type to normal short string }
  112. Message(parser_w_cdecl_no_openstring);
  113. vartype:=cshortstringtype;
  114. end;
  115. if assigned(indexnext) and
  116. (tsym(indexnext).typ=varsym) and
  117. (copy(tvarsym(indexnext).name,1,4)='high') then
  118. begin
  119. { removing it is too complicated,
  120. we just hide it PM }
  121. highname:='hidden'+copy(tvarsym(indexnext).name,5,high(name));
  122. Message(parser_w_cdecl_has_no_high);
  123. owner.rename(tvarsym(indexnext).name,highname);
  124. if is_array_of_const(vartype.def) and
  125. assigned(indexnext.indexnext) then
  126. Message(parser_e_C_array_of_const_must_be_last);
  127. end
  128. else
  129. begin
  130. if is_array_of_const(vartype.def) and
  131. assigned(indexnext) then
  132. Message(parser_e_C_array_of_const_must_be_last);
  133. end;
  134. end;
  135. end;
  136. end;
  137. procedure parameter_dec(aktprocdef:tabstractprocdef);
  138. {
  139. handle_procvar needs the same changes
  140. }
  141. var
  142. is_procvar : boolean;
  143. sc : tsinglelist;
  144. htype,
  145. tt : ttype;
  146. arrayelementtype : ttype;
  147. hvs,
  148. vs : tvarsym;
  149. srsym : tsym;
  150. hs1 : string;
  151. varspez : Tvarspez;
  152. inserthigh : boolean;
  153. tdefaultvalue : tconstsym;
  154. defaultrequired : boolean;
  155. old_object_option : tsymoptions;
  156. dummyst : tparasymtable;
  157. currparast : tparasymtable;
  158. begin
  159. consume(_LKLAMMER);
  160. { Delphi/Kylix supports nonsense like }
  161. { procedure p(); }
  162. if try_to_consume(_RKLAMMER) and
  163. not(m_tp7 in aktmodeswitches) then
  164. exit;
  165. { parsing a proc or procvar ? }
  166. is_procvar:=(aktprocdef.deftype=procvardef);
  167. { create dummy symtable for procvars }
  168. if is_procvar then
  169. begin
  170. dummyst:=tparasymtable.create;
  171. currparast:=dummyst;
  172. end
  173. else
  174. begin
  175. currparast:=tparasymtable(tprocdef(aktprocdef).parast);
  176. end;
  177. { reset }
  178. sc:=tsinglelist.create;
  179. defaultrequired:=false;
  180. { the variables are always public }
  181. old_object_option:=current_object_option;
  182. current_object_option:=[sp_public];
  183. inc(testcurobject);
  184. repeat
  185. if try_to_consume(_VAR) then
  186. varspez:=vs_var
  187. else
  188. if try_to_consume(_CONST) then
  189. varspez:=vs_const
  190. else
  191. if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  192. begin
  193. consume(_OUT);
  194. varspez:=vs_out
  195. end
  196. else
  197. varspez:=vs_value;
  198. inserthigh:=false;
  199. tdefaultvalue:=nil;
  200. tt.reset;
  201. { self is only allowed in procvars and class methods }
  202. if (idtoken=_SELF) and
  203. (is_procvar or
  204. (assigned(procinfo._class) and is_class(procinfo._class))) then
  205. begin
  206. if varspez <> vs_value then
  207. CGMessage(parser_e_self_call_by_value);
  208. if not is_procvar then
  209. begin
  210. htype.setdef(procinfo._class);
  211. vs:=tvarsym.create('@',htype);
  212. vs.varspez:=vs_var;
  213. { insert the sym in the parasymtable }
  214. tprocdef(aktprocdef).parast.insert(vs);
  215. inc(procinfo.selfpointer_offset,vs.address);
  216. end
  217. else
  218. vs:=nil;
  219. { must also be included for procvars to allow the proc2procvar }
  220. { type conversions (po_containsself is in po_comp) (JM) }
  221. include(aktprocdef.procoptions,po_containsself);
  222. consume(idtoken);
  223. consume(_COLON);
  224. single_type(tt,hs1,false);
  225. { this must be call-by-value, but we generate already an }
  226. { an error above if that's not the case (JM) }
  227. aktprocdef.concatpara(tt,vs,varspez,nil);
  228. { check the types for procedures only }
  229. if not is_procvar then
  230. begin
  231. if compare_defs(tt.def,procinfo._class,nothingn)=te_incompatible then
  232. CGMessage2(type_e_incompatible_types,tt.def.typename,procinfo._class.typename);
  233. end;
  234. end
  235. else
  236. begin
  237. { read identifiers and insert with error type }
  238. sc.reset;
  239. repeat
  240. vs:=tvarsym.create(orgpattern,generrortype);
  241. currparast.insert(vs);
  242. sc.insert(vs);
  243. consume(_ID);
  244. until not try_to_consume(_COMMA);
  245. { read type declaration, force reading for value and const paras }
  246. if (token=_COLON) or (varspez=vs_value) then
  247. begin
  248. consume(_COLON);
  249. { check for an open array }
  250. if token=_ARRAY then
  251. begin
  252. consume(_ARRAY);
  253. consume(_OF);
  254. { define range and type of range }
  255. tt.setdef(tarraydef.create(0,-1,s32bittype));
  256. { array of const ? }
  257. if (token=_CONST) and (m_objpas in aktmodeswitches) then
  258. begin
  259. consume(_CONST);
  260. srsym:=searchsymonlyin(systemunit,'TVARREC');
  261. if not assigned(srsym) then
  262. InternalError(1234124);
  263. tarraydef(tt.def).setelementtype(ttypesym(srsym).restype);
  264. tarraydef(tt.def).IsArrayOfConst:=true;
  265. end
  266. else
  267. begin
  268. { define field type }
  269. single_type(arrayelementtype,hs1,false);
  270. tarraydef(tt.def).setelementtype(arrayelementtype);
  271. end;
  272. inserthigh:=true;
  273. end
  274. else
  275. begin
  276. { open string ? }
  277. if (varspez=vs_var) and
  278. (
  279. (
  280. ((token=_STRING) or (idtoken=_SHORTSTRING)) and
  281. (cs_openstring in aktmoduleswitches) and
  282. not(cs_ansistrings in aktlocalswitches)
  283. ) or
  284. (idtoken=_OPENSTRING)) then
  285. begin
  286. consume(token);
  287. tt:=openshortstringtype;
  288. hs1:='openstring';
  289. inserthigh:=true;
  290. end
  291. else
  292. begin
  293. { everything else }
  294. single_type(tt,hs1,false);
  295. end;
  296. { default parameter }
  297. if (m_default_para in aktmodeswitches) then
  298. begin
  299. if try_to_consume(_EQUAL) then
  300. begin
  301. vs:=tvarsym(sc.first);
  302. if assigned(vs.listnext) then
  303. Message(parser_e_default_value_only_one_para);
  304. { prefix 'def' to the parameter name }
  305. tdefaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  306. if assigned(tdefaultvalue) then
  307. tprocdef(aktprocdef).parast.insert(tdefaultvalue);
  308. defaultrequired:=true;
  309. end
  310. else
  311. begin
  312. if defaultrequired then
  313. Message1(parser_e_default_value_expected_for_para,vs.name);
  314. end;
  315. end;
  316. end;
  317. end
  318. else
  319. begin
  320. {$ifndef UseNiceNames}
  321. hs1:='$$$';
  322. {$else UseNiceNames}
  323. hs1:='var';
  324. {$endif UseNiceNames}
  325. tt:=cformaltype;
  326. end;
  327. { For proc vars we only need the definitions }
  328. if not is_procvar then
  329. begin
  330. vs:=tvarsym(sc.first);
  331. while assigned(vs) do
  332. begin
  333. { update varsym }
  334. vs.vartype:=tt;
  335. vs.varspez:=varspez;
  336. if (varspez in [vs_var,vs_const,vs_out]) and
  337. paramanager.push_addr_param(tt.def,aktprocdef.proccalloption) then
  338. include(vs.varoptions,vo_regable);
  339. { also need to push a high value? }
  340. if inserthigh then
  341. begin
  342. hvs:=tvarsym.create('$high'+vs.name,s32bittype);
  343. hvs.varspez:=vs_const;
  344. {$ifdef vs_hidden}
  345. aktprocdef.concatpara(s32bittype,hvs,vs_hidden,nil);
  346. {$endif vs_hidden}
  347. currparast.insert(hvs);
  348. end;
  349. aktprocdef.concatpara(tt,vs,varspez,tdefaultvalue);
  350. vs:=tvarsym(vs.listnext);
  351. end;
  352. end
  353. else
  354. begin
  355. vs:=tvarsym(sc.first);
  356. while assigned(vs) do
  357. begin
  358. { don't insert a parasym, the varsyms will be
  359. disposed }
  360. aktprocdef.concatpara(tt,nil,varspez,tdefaultvalue);
  361. vs:=tvarsym(vs.listnext);
  362. end;
  363. end;
  364. end;
  365. { set the new mangled name }
  366. until not try_to_consume(_SEMICOLON);
  367. { remove parasymtable from stack }
  368. if is_procvar then
  369. dummyst.free;
  370. sc.free;
  371. { reset object options }
  372. dec(testcurobject);
  373. current_object_option:=old_object_option;
  374. consume(_RKLAMMER);
  375. end;
  376. procedure parse_proc_head(options:tproctypeoption);
  377. var
  378. orgsp,sp:stringid;
  379. paramoffset:longint;
  380. sym:tsym;
  381. st : tsymtable;
  382. srsymtable : tsymtable;
  383. storepos,procstartfilepos : tfileposinfo;
  384. i: longint;
  385. begin
  386. { Save the position where this procedure really starts }
  387. procstartfilepos:=akttokenpos;
  388. aktprocdef:=nil;
  389. if (options=potype_operator) then
  390. begin
  391. sp:=overloaded_names[optoken];
  392. orgsp:=sp;
  393. end
  394. else
  395. begin
  396. sp:=pattern;
  397. orgsp:=orgpattern;
  398. consume(_ID);
  399. end;
  400. { examine interface map: function/procedure iname.functionname=locfuncname }
  401. if parse_only and
  402. assigned(procinfo._class) and
  403. assigned(procinfo._class.implementedinterfaces) and
  404. (procinfo._class.implementedinterfaces.count>0) and
  405. try_to_consume(_POINT) then
  406. begin
  407. storepos:=akttokenpos;
  408. akttokenpos:=procstartfilepos;
  409. { get interface syms}
  410. searchsym(sp,sym,srsymtable);
  411. if not assigned(sym) then
  412. begin
  413. identifier_not_found(orgsp);
  414. sym:=generrorsym;
  415. end;
  416. akttokenpos:=storepos;
  417. { load proc name }
  418. if sym.typ=typesym then
  419. i:=procinfo._class.implementedinterfaces.searchintf(ttypesym(sym).restype.def);
  420. { qualifier is interface name? }
  421. if (sym.typ<>typesym) or (ttypesym(sym).restype.def.deftype<>objectdef) or
  422. (i=-1) then
  423. begin
  424. Message(parser_e_interface_id_expected);
  425. aktprocsym:=nil;
  426. end
  427. else
  428. begin
  429. aktprocsym:=tprocsym(procinfo._class.implementedinterfaces.interfaces(i).symtable.search(sp));
  430. { the method can be declared after the mapping FK
  431. if not(assigned(aktprocsym)) then
  432. Message(parser_e_methode_id_expected);
  433. }
  434. end;
  435. consume(_ID);
  436. consume(_EQUAL);
  437. if (token=_ID) { and assigned(aktprocsym) } then
  438. procinfo._class.implementedinterfaces.addmappings(i,sp,pattern);
  439. consume(_ID);
  440. exit;
  441. end;
  442. { method ? }
  443. if not(parse_only) and
  444. (lexlevel=normal_function_level) and
  445. try_to_consume(_POINT) then
  446. begin
  447. { search for object name }
  448. storepos:=akttokenpos;
  449. akttokenpos:=procstartfilepos;
  450. searchsym(sp,sym,srsymtable);
  451. if not assigned(sym) then
  452. begin
  453. identifier_not_found(orgsp);
  454. sym:=generrorsym;
  455. end;
  456. akttokenpos:=storepos;
  457. { consume proc name }
  458. sp:=pattern;
  459. orgsp:=orgpattern;
  460. procstartfilepos:=akttokenpos;
  461. consume(_ID);
  462. { qualifier is class name ? }
  463. if (sym.typ<>typesym) or
  464. (ttypesym(sym).restype.def.deftype<>objectdef) then
  465. begin
  466. Message(parser_e_class_id_expected);
  467. aktprocsym:=nil;
  468. aktprocdef:=nil;
  469. end
  470. else
  471. begin
  472. { used to allow private syms to be seen }
  473. aktobjectdef:=tobjectdef(ttypesym(sym).restype.def);
  474. procinfo._class:=tobjectdef(ttypesym(sym).restype.def);
  475. aktprocsym:=tprocsym(procinfo._class.symtable.search(sp));
  476. {The procedure has been found. So it is
  477. a global one. Set the flags to mark this.}
  478. procinfo.flags:=procinfo.flags or pi_is_global;
  479. aktobjectdef:=nil;
  480. { we solve this below }
  481. if not(assigned(aktprocsym)) then
  482. Message(parser_e_methode_id_expected);
  483. end;
  484. end
  485. else
  486. begin
  487. { check for constructor/destructor which is not allowed here }
  488. if (not parse_only) and
  489. (options in [potype_constructor,potype_destructor]) then
  490. Message(parser_e_constructors_always_objects);
  491. akttokenpos:=procstartfilepos;
  492. aktprocsym:=tprocsym(symtablestack.search(sp));
  493. if not(parse_only) then
  494. begin
  495. {The procedure we prepare for is in the implementation
  496. part of the unit we compile. It is also possible that we
  497. are compiling a program, which is also some kind of
  498. implementaion part.
  499. We need to find out if the procedure is global. If it is
  500. global, it is in the global symtable.}
  501. if not assigned(aktprocsym) and
  502. (symtablestack.symtabletype=staticsymtable) and
  503. assigned(symtablestack.next) and
  504. (symtablestack.next.unitid=0) then
  505. begin
  506. {Search the procedure in the global symtable.}
  507. aktprocsym:=tprocsym(symtablestack.next.search(sp));
  508. if assigned(aktprocsym) then
  509. begin
  510. {Check if it is a procedure.}
  511. if aktprocsym.typ<>procsym then
  512. DuplicateSym(aktprocsym);
  513. {The procedure has been found. So it is
  514. a global one. Set the flags to mark this.}
  515. procinfo.flags:=procinfo.flags or pi_is_global;
  516. end;
  517. end;
  518. end;
  519. end;
  520. if assigned(aktprocsym) then
  521. begin
  522. { Check if overloaded is a procsym }
  523. if aktprocsym.typ<>procsym then
  524. begin
  525. { when the other symbol is a unit symbol then hide the unit
  526. symbol. Only in tp mode because it's bad programming }
  527. if (m_duplicate_names in aktmodeswitches) and
  528. (aktprocsym.typ=unitsym) then
  529. begin
  530. aktprocsym.owner.rename(aktprocsym.name,'hidden'+aktprocsym.name);
  531. end
  532. else
  533. begin
  534. { we use a different error message for tp7 so it looks more compatible }
  535. if (m_fpc in aktmodeswitches) then
  536. Message1(parser_e_overloaded_no_procedure,aktprocsym.realname)
  537. else
  538. DuplicateSym(aktprocsym);
  539. { rename the name to an unique name to avoid an
  540. error when inserting the symbol in the symtable }
  541. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  542. end;
  543. { generate a new aktprocsym }
  544. aktprocsym:=nil;
  545. end;
  546. end;
  547. { test again if assigned, it can be reset to recover }
  548. if not assigned(aktprocsym) then
  549. begin
  550. { create a new procsym and set the real filepos }
  551. akttokenpos:=procstartfilepos;
  552. { for operator we have only one procsym for each overloaded
  553. operation }
  554. if (options=potype_operator) then
  555. begin
  556. { is the current overload sym already in the current unit }
  557. if assigned(overloaded_operators[optoken]) and
  558. (overloaded_operators[optoken].owner=symtablestack) then
  559. aktprocsym:=overloaded_operators[optoken]
  560. else
  561. begin
  562. { create the procsym with saving the original case }
  563. aktprocsym:=tprocsym.create('$'+sp);
  564. if assigned(overloaded_operators[optoken]) then
  565. overloaded_operators[optoken].concat_procdefs_to(aktprocsym);
  566. end;
  567. end
  568. else
  569. aktprocsym:=tprocsym.create(orgsp);
  570. symtablestack.insert(aktprocsym);
  571. end;
  572. st:=symtablestack;
  573. aktprocdef:=tprocdef.create;
  574. aktprocdef.symtablelevel:=symtablestack.symtablelevel;
  575. if assigned(procinfo._class) then
  576. aktprocdef._class := procinfo._class;
  577. { set the options from the caller (podestructor or poconstructor) }
  578. aktprocdef.proctypeoption:=options;
  579. { add procsym to the procdef }
  580. aktprocdef.procsym:=aktprocsym;
  581. { save file position }
  582. aktprocdef.fileinfo:=procstartfilepos;
  583. { this must also be inserted in the right symtable !! PM }
  584. { otherwise we get subbtle problems with
  585. definitions of args defs in staticsymtable for
  586. implementation of a global method }
  587. if token=_LKLAMMER then
  588. parameter_dec(aktprocdef);
  589. { calculate the offset of the parameters }
  590. paramoffset:=target_info.first_parm_offset;
  591. { calculate frame pointer offset }
  592. if lexlevel>normal_function_level then
  593. begin
  594. procinfo.framepointer_offset:=paramoffset;
  595. inc(paramoffset,pointer_size);
  596. { this is needed to get correct framepointer push for local
  597. forward functions !! }
  598. aktprocdef.parast.symtablelevel:=lexlevel;
  599. end;
  600. if assigned (procinfo._Class) and
  601. is_object(procinfo._Class) and
  602. (aktprocdef.proctypeoption in [potype_constructor,potype_destructor]) then
  603. inc(paramoffset,pointer_size);
  604. { self pointer offset, must be done after parsing the parameters }
  605. { self isn't pushed in nested procedure of methods }
  606. if assigned(procinfo._class) and (lexlevel=normal_function_level) then
  607. begin
  608. procinfo.selfpointer_offset:=paramoffset;
  609. if assigned(aktprocdef) and
  610. not(po_containsself in aktprocdef.procoptions) then
  611. inc(paramoffset,pointer_size);
  612. end;
  613. { con/-destructor flag ? }
  614. if assigned (procinfo._Class) and
  615. is_class(procinfo._class) and
  616. (aktprocdef.proctypeoption in [potype_destructor,potype_constructor]) then
  617. inc(paramoffset,pointer_size);
  618. procinfo.para_offset:=paramoffset;
  619. { so we only restore the symtable now }
  620. symtablestack:=st;
  621. if (options=potype_operator) then
  622. overloaded_operators[optoken]:=aktprocsym;
  623. end;
  624. procedure parse_proc_dec;
  625. var
  626. hs : string;
  627. isclassmethod : boolean;
  628. begin
  629. inc(lexlevel);
  630. { read class method }
  631. if token=_CLASS then
  632. begin
  633. consume(_CLASS);
  634. isclassmethod:=true;
  635. end
  636. else
  637. isclassmethod:=false;
  638. case token of
  639. _FUNCTION : begin
  640. consume(_FUNCTION);
  641. parse_proc_head(potype_none);
  642. if token<>_COLON then
  643. begin
  644. if assigned(aktprocsym) and
  645. not(is_interface(aktprocdef._class)) and
  646. not(aktprocdef.forwarddef) or
  647. (m_repeat_forward in aktmodeswitches) then
  648. begin
  649. consume(_COLON);
  650. consume_all_until(_SEMICOLON);
  651. end;
  652. end
  653. else
  654. begin
  655. consume(_COLON);
  656. inc(testcurobject);
  657. single_type(aktprocdef.rettype,hs,false);
  658. aktprocdef.test_if_fpu_result;
  659. if (aktprocdef.rettype.def.deftype=stringdef) and
  660. (tstringdef(aktprocdef.rettype.def).string_typ<>st_shortstring) then
  661. procinfo.no_fast_exit:=true;
  662. dec(testcurobject);
  663. end;
  664. end;
  665. _PROCEDURE : begin
  666. consume(_PROCEDURE);
  667. parse_proc_head(potype_none);
  668. if assigned(aktprocsym) then
  669. aktprocdef.rettype:=voidtype;
  670. end;
  671. _CONSTRUCTOR : begin
  672. consume(_CONSTRUCTOR);
  673. parse_proc_head(potype_constructor);
  674. if assigned(procinfo._class) and
  675. is_class(procinfo._class) then
  676. begin
  677. { CLASS constructors return the created instance }
  678. aktprocdef.rettype.setdef(procinfo._class);
  679. end
  680. else
  681. begin
  682. { OBJECT constructors return a boolean }
  683. aktprocdef.rettype:=booltype;
  684. end;
  685. end;
  686. _DESTRUCTOR : begin
  687. consume(_DESTRUCTOR);
  688. parse_proc_head(potype_destructor);
  689. aktprocdef.rettype:=voidtype;
  690. end;
  691. _OPERATOR : begin
  692. if lexlevel>normal_function_level then
  693. Message(parser_e_no_local_operator);
  694. consume(_OPERATOR);
  695. if (token in [first_overloaded..last_overloaded]) then
  696. begin
  697. procinfo.flags:=procinfo.flags or pi_operator;
  698. optoken:=token;
  699. end
  700. else
  701. begin
  702. Message(parser_e_overload_operator_failed);
  703. { Use the dummy NOTOKEN that is also declared
  704. for the overloaded_operator[] }
  705. optoken:=NOTOKEN;
  706. end;
  707. consume(Token);
  708. parse_proc_head(potype_operator);
  709. if token<>_ID then
  710. begin
  711. otsym:=nil;
  712. if not(m_result in aktmodeswitches) then
  713. consume(_ID);
  714. end
  715. else
  716. begin
  717. otsym:=tvarsym.create(pattern,voidtype);
  718. consume(_ID);
  719. end;
  720. if not try_to_consume(_COLON) then
  721. begin
  722. consume(_COLON);
  723. aktprocdef.rettype:=generrortype;
  724. consume_all_until(_SEMICOLON);
  725. end
  726. else
  727. begin
  728. single_type(aktprocdef.rettype,hs,false);
  729. aktprocdef.test_if_fpu_result;
  730. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  731. ((aktprocdef.rettype.def.deftype<>orddef) or
  732. (torddef(aktprocdef.rettype.def).typ<>bool8bit)) then
  733. Message(parser_e_comparative_operator_return_boolean);
  734. if assigned(otsym) then
  735. otsym.vartype.def:=aktprocdef.rettype.def;
  736. if (optoken=_ASSIGNMENT) and
  737. equal_defs(aktprocdef.rettype.def,
  738. tvarsym(aktprocdef.parast.symindex.first).vartype.def) then
  739. message(parser_e_no_such_assignment)
  740. else if not isoperatoracceptable(aktprocdef,optoken) then
  741. Message(parser_e_overload_impossible);
  742. end;
  743. end;
  744. end;
  745. if isclassmethod and
  746. assigned(aktprocsym) then
  747. include(aktprocdef.procoptions,po_classmethod);
  748. { support procedure proc;stdcall export; in Delphi mode only }
  749. if not((m_delphi in aktmodeswitches) and
  750. is_proc_directive(token)) then
  751. consume(_SEMICOLON);
  752. dec(lexlevel);
  753. end;
  754. {****************************************************************************
  755. Procedure directive handlers
  756. ****************************************************************************}
  757. procedure pd_far;
  758. begin
  759. Message1(parser_w_proc_directive_ignored,'FAR');
  760. end;
  761. procedure pd_near;
  762. begin
  763. Message1(parser_w_proc_directive_ignored,'NEAR');
  764. end;
  765. procedure pd_export;
  766. begin
  767. if assigned(procinfo._class) then
  768. Message(parser_e_methods_dont_be_export);
  769. if lexlevel<>normal_function_level then
  770. Message(parser_e_dont_nest_export);
  771. { only os/2 needs this }
  772. if target_info.system=system_i386_os2 then
  773. begin
  774. aktprocdef.aliasnames.insert(aktprocsym.realname);
  775. procinfo.exported:=true;
  776. if cs_link_deffile in aktglobalswitches then
  777. deffile.AddExport(aktprocdef.mangledname);
  778. end;
  779. end;
  780. procedure pd_forward;
  781. begin
  782. aktprocdef.forwarddef:=true;
  783. end;
  784. procedure pd_alias;
  785. begin
  786. consume(_COLON);
  787. aktprocdef.aliasnames.insert(get_stringconst);
  788. end;
  789. procedure pd_asmname;
  790. begin
  791. aktprocdef.setmangledname(target_info.Cprefix+pattern);
  792. if token=_CCHAR then
  793. consume(_CCHAR)
  794. else
  795. consume(_CSTRING);
  796. { we don't need anything else }
  797. aktprocdef.forwarddef:=false;
  798. end;
  799. procedure pd_inline;
  800. var
  801. hp : tparaitem;
  802. begin
  803. { check if there is an array of const }
  804. hp:=tparaitem(aktprocdef.para.first);
  805. while assigned(hp) do
  806. begin
  807. if assigned(hp.paratype.def) and
  808. (hp.paratype.def.deftype=arraydef) then
  809. begin
  810. with tarraydef(hp.paratype.def) do
  811. if IsVariant or IsConstructor {or IsArrayOfConst} then
  812. begin
  813. Message1(parser_w_not_supported_for_inline,'array of const');
  814. Message(parser_w_inlining_disabled);
  815. aktprocdef.proccalloption:=pocall_fpccall;
  816. end;
  817. end;
  818. hp:=tparaitem(hp.next);
  819. end;
  820. end;
  821. procedure pd_intern;
  822. begin
  823. consume(_COLON);
  824. aktprocdef.extnumber:=get_intconst;
  825. end;
  826. procedure pd_interrupt;
  827. begin
  828. if lexlevel<>normal_function_level then
  829. Message(parser_e_dont_nest_interrupt);
  830. end;
  831. procedure pd_abstract;
  832. begin
  833. if (po_virtualmethod in aktprocdef.procoptions) then
  834. include(aktprocdef.procoptions,po_abstractmethod)
  835. else
  836. Message(parser_e_only_virtual_methods_abstract);
  837. { the method is defined }
  838. aktprocdef.forwarddef:=false;
  839. end;
  840. procedure pd_virtual;
  841. {$ifdef WITHDMT}
  842. var
  843. pt : tnode;
  844. {$endif WITHDMT}
  845. begin
  846. if (aktprocdef.proctypeoption=potype_constructor) and
  847. is_object(aktprocdef._class) then
  848. Message(parser_e_constructor_cannot_be_not_virtual);
  849. {$ifdef WITHDMT}
  850. if is_object(aktprocdef._class) and
  851. (token<>_SEMICOLON) then
  852. begin
  853. { any type of parameter is allowed here! }
  854. pt:=comp_expr(true);
  855. if is_constintnode(pt) then
  856. begin
  857. include(aktprocdef.procoptions,po_msgint);
  858. aktprocdef.messageinf.i:=pt^.value;
  859. end
  860. else
  861. Message(parser_e_ill_msg_expr);
  862. disposetree(pt);
  863. end;
  864. {$endif WITHDMT}
  865. end;
  866. procedure pd_static;
  867. begin
  868. if (cs_static_keyword in aktmoduleswitches) then
  869. begin
  870. include(aktprocsym.symoptions,sp_static);
  871. include(aktprocdef.procoptions,po_staticmethod);
  872. end;
  873. end;
  874. procedure pd_override;
  875. begin
  876. if not(is_class_or_interface(aktprocdef._class)) then
  877. Message(parser_e_no_object_override);
  878. end;
  879. procedure pd_overload;
  880. begin
  881. include(aktprocsym.symoptions,sp_has_overloaded);
  882. end;
  883. procedure pd_message;
  884. var
  885. pt : tnode;
  886. begin
  887. { check parameter type }
  888. if not(po_containsself in aktprocdef.procoptions) and
  889. ((aktprocdef.minparacount<>1) or
  890. (aktprocdef.maxparacount<>1) or
  891. (TParaItem(aktprocdef.Para.first).paratyp<>vs_var)) then
  892. Message(parser_e_ill_msg_param);
  893. pt:=comp_expr(true);
  894. if pt.nodetype=stringconstn then
  895. begin
  896. include(aktprocdef.procoptions,po_msgstr);
  897. aktprocdef.messageinf.str:=strnew(tstringconstnode(pt).value_str);
  898. end
  899. else
  900. if is_constintnode(pt) then
  901. begin
  902. include(aktprocdef.procoptions,po_msgint);
  903. aktprocdef.messageinf.i:=tordconstnode(pt).value;
  904. end
  905. else
  906. Message(parser_e_ill_msg_expr);
  907. pt.free;
  908. end;
  909. procedure pd_reintroduce;
  910. begin
  911. Message1(parser_w_proc_directive_ignored,'REINTRODUCE');
  912. end;
  913. procedure pd_syscall;
  914. begin
  915. aktprocdef.forwarddef:=false;
  916. aktprocdef.extnumber:=get_intconst;
  917. end;
  918. procedure pd_external;
  919. {
  920. If import_dll=nil the procedure is assumed to be in another
  921. object file. In that object file it should have the name to
  922. which import_name is pointing to. Otherwise, the procedure is
  923. assumed to be in the DLL to which import_dll is pointing to. In
  924. that case either import_nr<>0 or import_name<>nil is true, so
  925. the procedure is either imported by number or by name. (DM)
  926. }
  927. var
  928. pd : tprocdef;
  929. import_dll,
  930. import_name : string;
  931. import_nr : word;
  932. begin
  933. aktprocdef.forwarddef:=false;
  934. { forbid local external procedures }
  935. if lexlevel>normal_function_level then
  936. Message(parser_e_no_local_external);
  937. { If the procedure should be imported from a DLL, a constant string follows.
  938. This isn't really correct, an contant string expression follows
  939. so we check if an semicolon follows, else a string constant have to
  940. follow (FK) }
  941. import_nr:=0;
  942. import_name:='';
  943. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  944. begin
  945. import_dll:=get_stringconst;
  946. if (idtoken=_NAME) then
  947. begin
  948. consume(_NAME);
  949. import_name:=get_stringconst;
  950. end;
  951. if (idtoken=_INDEX) then
  952. begin
  953. {After the word index follows the index number in the DLL.}
  954. consume(_INDEX);
  955. import_nr:=get_intconst;
  956. end;
  957. { default is to used the realname of the procedure }
  958. if (import_nr=0) and (import_name='') then
  959. import_name:=aktprocsym.realname;
  960. { create importlib if not already done }
  961. if not(current_module.uses_imports) then
  962. begin
  963. current_module.uses_imports:=true;
  964. importlib.preparelib(current_module.modulename^);
  965. end;
  966. if not(m_repeat_forward in aktmodeswitches) then
  967. begin
  968. { we can only have one overloaded here ! }
  969. if aktprocsym.procdef_count>1 then
  970. pd:=aktprocsym.procdef[2]
  971. else
  972. pd:=aktprocdef;
  973. end
  974. else
  975. pd:=aktprocdef;
  976. importlib.importproceduredef(pd,import_dll,import_nr,import_name);
  977. end
  978. else
  979. begin
  980. if (idtoken=_NAME) then
  981. begin
  982. consume(_NAME);
  983. import_name:=get_stringconst;
  984. aktprocdef.setmangledname(import_name);
  985. end;
  986. end;
  987. end;
  988. type
  989. pd_handler=procedure;
  990. proc_dir_rec=record
  991. idtok : ttoken;
  992. pd_flags : longint;
  993. handler : pd_handler;
  994. pocall : tproccalloption;
  995. pooption : tprocoptions;
  996. mutexclpocall : tproccalloptions;
  997. mutexclpotype : tproctypeoptions;
  998. mutexclpo : tprocoptions;
  999. end;
  1000. const
  1001. {Should contain the number of procedure directives we support.}
  1002. num_proc_directives=36;
  1003. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1004. (
  1005. (
  1006. idtok:_ABSTRACT;
  1007. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1008. handler : {$ifdef FPCPROCVAR}@{$endif}pd_abstract;
  1009. pocall : pocall_none;
  1010. pooption : [po_abstractmethod];
  1011. mutexclpocall : [pocall_internproc,pocall_inline];
  1012. mutexclpotype : [potype_constructor,potype_destructor];
  1013. mutexclpo : [po_exports,po_interrupt,po_external]
  1014. ),(
  1015. idtok:_ALIAS;
  1016. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1017. handler : {$ifdef FPCPROCVAR}@{$endif}pd_alias;
  1018. pocall : pocall_none;
  1019. pooption : [];
  1020. mutexclpocall : [pocall_inline];
  1021. mutexclpotype : [];
  1022. mutexclpo : [po_external]
  1023. ),(
  1024. idtok:_ASMNAME;
  1025. pd_flags : pd_interface+pd_implemen+pd_notobjintf;
  1026. handler : {$ifdef FPCPROCVAR}@{$endif}pd_asmname;
  1027. pocall : pocall_cdecl;
  1028. pooption : [po_external];
  1029. mutexclpocall : [pocall_internproc,pocall_inline];
  1030. mutexclpotype : [];
  1031. mutexclpo : [po_external]
  1032. ),(
  1033. idtok:_ASSEMBLER;
  1034. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1035. handler : nil;
  1036. pocall : pocall_none;
  1037. pooption : [po_assembler];
  1038. mutexclpocall : [];
  1039. mutexclpotype : [];
  1040. mutexclpo : [po_external]
  1041. ),(
  1042. idtok:_CDECL;
  1043. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1044. handler : nil;
  1045. pocall : pocall_cdecl;
  1046. pooption : [];
  1047. mutexclpocall : [];
  1048. mutexclpotype : [];
  1049. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1050. ),(
  1051. idtok:_DYNAMIC;
  1052. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1053. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1054. pocall : pocall_none;
  1055. pooption : [po_virtualmethod];
  1056. mutexclpocall : [pocall_internproc,pocall_inline];
  1057. mutexclpotype : [];
  1058. mutexclpo : [po_exports,po_interrupt,po_external]
  1059. ),(
  1060. idtok:_EXPORT;
  1061. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??}+pd_notobjintf;
  1062. handler : {$ifdef FPCPROCVAR}@{$endif}pd_export;
  1063. pocall : pocall_none;
  1064. pooption : [po_exports];
  1065. mutexclpocall : [pocall_internproc,pocall_inline];
  1066. mutexclpotype : [];
  1067. mutexclpo : [po_external,po_interrupt]
  1068. ),(
  1069. idtok:_EXTERNAL;
  1070. pd_flags : pd_implemen+pd_interface+pd_notobject+pd_notobjintf;
  1071. handler : {$ifdef FPCPROCVAR}@{$endif}pd_external;
  1072. pocall : pocall_none;
  1073. pooption : [po_external];
  1074. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  1075. mutexclpotype : [];
  1076. mutexclpo : [po_exports,po_interrupt,po_assembler]
  1077. ),(
  1078. idtok:_FAR;
  1079. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar+pd_notobject+pd_notobjintf;
  1080. handler : {$ifdef FPCPROCVAR}@{$endif}pd_far;
  1081. pocall : pocall_none;
  1082. pooption : [];
  1083. mutexclpocall : [pocall_internproc,pocall_inline];
  1084. mutexclpotype : [];
  1085. mutexclpo : []
  1086. ),(
  1087. idtok:_FAR16;
  1088. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar+pd_notobject;
  1089. handler : nil;
  1090. pocall : pocall_far16;
  1091. pooption : [];
  1092. mutexclpocall : [];
  1093. mutexclpotype : [];
  1094. mutexclpo : [po_external,po_leftright]
  1095. ),(
  1096. idtok:_FORWARD;
  1097. pd_flags : pd_implemen+pd_notobject+pd_notobjintf;
  1098. handler : {$ifdef FPCPROCVAR}@{$endif}pd_forward;
  1099. pocall : pocall_none;
  1100. pooption : [];
  1101. mutexclpocall : [pocall_internproc,pocall_inline];
  1102. mutexclpotype : [];
  1103. mutexclpo : [po_external]
  1104. ),(
  1105. idtok:_FPCCALL;
  1106. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1107. handler : nil;
  1108. pocall : pocall_fpccall;
  1109. pooption : [];
  1110. mutexclpocall : [];
  1111. mutexclpotype : [];
  1112. mutexclpo : [po_leftright]
  1113. ),(
  1114. idtok:_INLINE;
  1115. pd_flags : pd_interface+pd_implemen+pd_body+pd_notobjintf;
  1116. handler : {$ifdef FPCPROCVAR}@{$endif}pd_inline;
  1117. pocall : pocall_inline;
  1118. pooption : [];
  1119. mutexclpocall : [];
  1120. mutexclpotype : [potype_constructor,potype_destructor];
  1121. mutexclpo : [po_exports,po_external,po_interrupt]
  1122. ),(
  1123. idtok:_INTERNCONST;
  1124. pd_flags : pd_implemen+pd_body+pd_notobject+pd_notobjintf;
  1125. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1126. pocall : pocall_none;
  1127. pooption : [po_internconst];
  1128. mutexclpocall : [];
  1129. mutexclpotype : [potype_operator];
  1130. mutexclpo : []
  1131. ),(
  1132. idtok:_INTERNPROC;
  1133. pd_flags : pd_implemen+pd_notobject+pd_notobjintf;
  1134. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1135. pocall : pocall_internproc;
  1136. pooption : [];
  1137. mutexclpocall : [];
  1138. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1139. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_leftright]
  1140. ),(
  1141. idtok:_INTERRUPT;
  1142. pd_flags : pd_implemen+pd_body+pd_notobject+pd_notobjintf;
  1143. handler : {$ifdef FPCPROCVAR}@{$endif}pd_interrupt;
  1144. pocall : pocall_none;
  1145. pooption : [po_interrupt];
  1146. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,
  1147. pocall_inline,pocall_pascal,pocall_system,pocall_far16,pocall_fpccall];
  1148. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1149. mutexclpo : [po_external,po_leftright,po_clearstack]
  1150. ),(
  1151. idtok:_IOCHECK;
  1152. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1153. handler : nil;
  1154. pocall : pocall_none;
  1155. pooption : [po_iocheck];
  1156. mutexclpocall : [pocall_internproc];
  1157. mutexclpotype : [];
  1158. mutexclpo : [po_external]
  1159. ),(
  1160. idtok:_MESSAGE;
  1161. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1162. handler : {$ifdef FPCPROCVAR}@{$endif}pd_message;
  1163. pocall : pocall_none;
  1164. pooption : []; { can be po_msgstr or po_msgint }
  1165. mutexclpocall : [pocall_inline,pocall_internproc];
  1166. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1167. mutexclpo : [po_interrupt,po_external]
  1168. ),(
  1169. idtok:_NEAR;
  1170. pd_flags : pd_implemen+pd_body+pd_procvar+pd_notobjintf;
  1171. handler : {$ifdef FPCPROCVAR}@{$endif}pd_near;
  1172. pocall : pocall_none;
  1173. pooption : [];
  1174. mutexclpocall : [pocall_internproc];
  1175. mutexclpotype : [];
  1176. mutexclpo : []
  1177. ),(
  1178. idtok:_OVERLOAD;
  1179. pd_flags : pd_implemen+pd_interface+pd_body;
  1180. handler : {$ifdef FPCPROCVAR}@{$endif}pd_overload;
  1181. pocall : pocall_none;
  1182. pooption : [po_overload];
  1183. mutexclpocall : [pocall_internproc];
  1184. mutexclpotype : [];
  1185. mutexclpo : []
  1186. ),(
  1187. idtok:_OVERRIDE;
  1188. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1189. handler : {$ifdef FPCPROCVAR}@{$endif}pd_override;
  1190. pocall : pocall_none;
  1191. pooption : [po_overridingmethod,po_virtualmethod];
  1192. mutexclpocall : [pocall_inline,pocall_internproc];
  1193. mutexclpotype : [];
  1194. mutexclpo : [po_exports,po_external,po_interrupt]
  1195. ),(
  1196. idtok:_PASCAL;
  1197. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1198. handler : nil;
  1199. pocall : pocall_pascal;
  1200. pooption : [];
  1201. mutexclpocall : [];
  1202. mutexclpotype : [];
  1203. mutexclpo : [po_external]
  1204. ),(
  1205. idtok:_POPSTACK;
  1206. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1207. handler : nil;
  1208. pocall : pocall_none;
  1209. pooption : [po_clearstack];
  1210. mutexclpocall : [pocall_inline,pocall_internproc,pocall_stdcall];
  1211. mutexclpotype : [];
  1212. mutexclpo : [po_assembler,po_external]
  1213. ),(
  1214. idtok:_PUBLIC;
  1215. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject+pd_notobjintf;
  1216. handler : nil;
  1217. pocall : pocall_none;
  1218. pooption : [];
  1219. mutexclpocall : [pocall_internproc,pocall_inline];
  1220. mutexclpotype : [];
  1221. mutexclpo : [po_external]
  1222. ),(
  1223. idtok:_REGISTER;
  1224. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1225. handler : nil;
  1226. pocall : pocall_register;
  1227. pooption : [];
  1228. mutexclpocall : [];
  1229. mutexclpotype : [];
  1230. mutexclpo : [po_external]
  1231. ),(
  1232. idtok:_REINTRODUCE;
  1233. pd_flags : pd_interface+pd_object;
  1234. handler : {$ifdef FPCPROCVAR}@{$endif}pd_reintroduce;
  1235. pocall : pocall_none;
  1236. pooption : [];
  1237. mutexclpocall : [];
  1238. mutexclpotype : [];
  1239. mutexclpo : []
  1240. ),(
  1241. idtok:_SAFECALL;
  1242. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1243. handler : nil;
  1244. pocall : pocall_safecall;
  1245. pooption : [];
  1246. mutexclpocall : [];
  1247. mutexclpotype : [];
  1248. mutexclpo : [po_external]
  1249. ),(
  1250. idtok:_SAVEREGISTERS;
  1251. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar+pd_notobjintf;
  1252. handler : nil;
  1253. pocall : pocall_none;
  1254. pooption : [po_saveregisters];
  1255. mutexclpocall : [pocall_internproc];
  1256. mutexclpotype : [];
  1257. mutexclpo : [po_external]
  1258. ),(
  1259. idtok:_STATIC;
  1260. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1261. handler : {$ifdef FPCPROCVAR}@{$endif}pd_static;
  1262. pocall : pocall_none;
  1263. pooption : [po_staticmethod];
  1264. mutexclpocall : [pocall_inline,pocall_internproc];
  1265. mutexclpotype : [potype_constructor,potype_destructor];
  1266. mutexclpo : [po_external,po_interrupt,po_exports]
  1267. ),(
  1268. idtok:_STDCALL;
  1269. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1270. handler : nil;
  1271. pocall : pocall_stdcall;
  1272. pooption : [];
  1273. mutexclpocall : [];
  1274. mutexclpotype : [];
  1275. mutexclpo : [po_external]
  1276. ),(
  1277. idtok:_SYSCALL;
  1278. pd_flags : pd_interface+pd_implemen+pd_notobjintf;
  1279. handler : {$ifdef FPCPROCVAR}@{$endif}pd_syscall;
  1280. pocall : pocall_palmossyscall;
  1281. pooption : [];
  1282. mutexclpocall : [];
  1283. mutexclpotype : [];
  1284. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1285. ),(
  1286. idtok:_SYSTEM;
  1287. pd_flags : pd_implemen+pd_notobject+pd_notobjintf;
  1288. handler : nil;
  1289. pocall : pocall_system;
  1290. pooption : [];
  1291. mutexclpocall : [];
  1292. mutexclpotype : [];
  1293. mutexclpo : [po_external,po_assembler,po_interrupt]
  1294. ),(
  1295. idtok:_VIRTUAL;
  1296. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1297. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1298. pocall : pocall_none;
  1299. pooption : [po_virtualmethod];
  1300. mutexclpocall : [pocall_inline,pocall_internproc];
  1301. mutexclpotype : [];
  1302. mutexclpo : [po_external,po_interrupt,po_exports]
  1303. ),(
  1304. idtok:_CPPDECL;
  1305. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1306. handler : nil;
  1307. pocall : pocall_cppdecl;
  1308. pooption : [po_savestdregs];
  1309. mutexclpocall : [];
  1310. mutexclpotype : [];
  1311. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1312. ),(
  1313. idtok:_VARARGS;
  1314. pd_flags : pd_interface+pd_implemen+pd_procvar;
  1315. handler : nil;
  1316. pocall : pocall_none;
  1317. pooption : [po_varargs];
  1318. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1319. pocall_inline,pocall_far16,pocall_fpccall];
  1320. mutexclpotype : [];
  1321. mutexclpo : [po_assembler,po_interrupt,po_leftright]
  1322. ),(
  1323. idtok:_COMPILERPROC;
  1324. pd_flags : pd_interface+pd_implemen+pd_body+pd_notobjintf;
  1325. handler : nil;
  1326. pocall : pocall_compilerproc;
  1327. pooption : [];
  1328. mutexclpocall : [];
  1329. mutexclpotype : [];
  1330. mutexclpo : [po_interrupt]
  1331. )
  1332. );
  1333. function is_proc_directive(tok:ttoken):boolean;
  1334. var
  1335. i : longint;
  1336. begin
  1337. is_proc_directive:=false;
  1338. for i:=1 to num_proc_directives do
  1339. if proc_direcdata[i].idtok=idtoken then
  1340. begin
  1341. is_proc_directive:=true;
  1342. exit;
  1343. end;
  1344. end;
  1345. function parse_proc_direc(var pdflags:word):boolean;
  1346. {
  1347. Parse the procedure directive, returns true if a correct directive is found
  1348. }
  1349. var
  1350. p : longint;
  1351. found : boolean;
  1352. name : stringid;
  1353. begin
  1354. parse_proc_direc:=false;
  1355. name:=tokeninfo^[idtoken].str;
  1356. found:=false;
  1357. { Hint directive? Then exit immediatly }
  1358. if (m_hintdirective in aktmodeswitches) then
  1359. begin
  1360. case idtoken of
  1361. _LIBRARY,
  1362. _PLATFORM,
  1363. _UNIMPLEMENTED,
  1364. _DEPRECATED :
  1365. exit;
  1366. end;
  1367. end;
  1368. { retrieve data for directive if found }
  1369. for p:=1 to num_proc_directives do
  1370. if proc_direcdata[p].idtok=idtoken then
  1371. begin
  1372. found:=true;
  1373. break;
  1374. end;
  1375. { Check if the procedure directive is known }
  1376. if not found then
  1377. begin
  1378. { parsing a procvar type the name can be any
  1379. next variable !! }
  1380. if (pdflags and (pd_procvar or pd_object))=0 then
  1381. Message1(parser_w_unknown_proc_directive_ignored,name);
  1382. exit;
  1383. end;
  1384. { static needs a special treatment }
  1385. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1386. exit;
  1387. { Conflicts between directives ? }
  1388. if (aktprocdef.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1389. (aktprocdef.proccalloption in proc_direcdata[p].mutexclpocall) or
  1390. ((aktprocdef.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1391. begin
  1392. Message1(parser_e_proc_dir_conflict,name);
  1393. exit;
  1394. end;
  1395. { set calling convention }
  1396. if proc_direcdata[p].pocall<>pocall_none then
  1397. begin
  1398. if aktprocdef.proccalloption<>pocall_none then
  1399. begin
  1400. Message2(parser_w_proc_overriding_calling,
  1401. proccalloptionStr[aktprocdef.proccalloption],
  1402. proccalloptionStr[proc_direcdata[p].pocall]);
  1403. end;
  1404. aktprocdef.proccalloption:=proc_direcdata[p].pocall;
  1405. end;
  1406. { check if method and directive not for object, like public.
  1407. This needs to be checked also for procvars }
  1408. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1409. (aktprocdef.owner.symtabletype=objectsymtable) then
  1410. exit;
  1411. if aktprocdef.deftype=procdef then
  1412. begin
  1413. { Check if the directive is only for objects }
  1414. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1415. not assigned(aktprocdef._class) then
  1416. exit;
  1417. { check if method and directive not for interface }
  1418. if ((proc_direcdata[p].pd_flags and pd_notobjintf)<>0) and
  1419. is_interface(aktprocdef._class) then
  1420. exit;
  1421. end;
  1422. { consume directive, and turn flag on }
  1423. consume(token);
  1424. parse_proc_direc:=true;
  1425. { Check the pd_flags if the directive should be allowed }
  1426. if ((pdflags and pd_interface)<>0) and
  1427. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1428. begin
  1429. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1430. exit;
  1431. end;
  1432. if ((pdflags and pd_implemen)<>0) and
  1433. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1434. begin
  1435. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1436. exit;
  1437. end;
  1438. if ((pdflags and pd_procvar)<>0) and
  1439. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1440. begin
  1441. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1442. exit;
  1443. end;
  1444. { Return the new pd_flags }
  1445. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1446. pdflags:=pdflags and (not pd_body);
  1447. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1448. pdflags:=pdflags or pd_global;
  1449. { Add the correct flag }
  1450. aktprocdef.procoptions:=aktprocdef.procoptions+proc_direcdata[p].pooption;
  1451. { Call the handler }
  1452. if pointer({$ifndef FPCPROCVAR}@{$endif}proc_direcdata[p].handler)<>nil then
  1453. proc_direcdata[p].handler{$ifdef FPCPROCVAR}(){$endif};
  1454. end;
  1455. procedure handle_calling_convention(sym:tprocsym;def:tabstractprocdef);
  1456. begin
  1457. { set the default calling convention }
  1458. if def.proccalloption=pocall_none then
  1459. def.proccalloption:=aktdefproccall;
  1460. { generate symbol names for local copies }
  1461. if (def.deftype=procdef) then
  1462. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara,nil);
  1463. { handle proccall specific settings }
  1464. case def.proccalloption of
  1465. pocall_cdecl :
  1466. begin
  1467. { use popstack and save std registers }
  1468. include(def.procoptions,po_clearstack);
  1469. include(def.procoptions,po_savestdregs);
  1470. { set mangledname }
  1471. if (def.deftype=procdef) then
  1472. begin
  1473. if not tprocdef(def).has_mangledname then
  1474. begin
  1475. if assigned(tprocdef(def)._class) then
  1476. tprocdef(def).setmangledname(target_info.Cprefix+tprocdef(def)._class.objrealname^+'_'+sym.realname)
  1477. else
  1478. tprocdef(def).setmangledname(target_info.Cprefix+sym.realname);
  1479. end;
  1480. if not assigned(tprocdef(def).parast) then
  1481. internalerror(200110234);
  1482. { check C cdecl para types }
  1483. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkparatype,nil);
  1484. { Adjust alignment to match cdecl or stdcall }
  1485. tprocdef(def).parast.dataalignment:=std_param_align;
  1486. end;
  1487. end;
  1488. pocall_cppdecl :
  1489. begin
  1490. if not assigned(sym) then
  1491. internalerror(200110231);
  1492. { use popstack and save std registers }
  1493. include(def.procoptions,po_clearstack);
  1494. include(def.procoptions,po_savestdregs);
  1495. { set mangledname }
  1496. if (def.deftype=procdef) then
  1497. begin
  1498. if not tprocdef(def).has_mangledname then
  1499. tprocdef(def).setmangledname(target_info.Cprefix+tprocdef(def).cplusplusmangledname);
  1500. if not assigned(tprocdef(def).parast) then
  1501. internalerror(200110235);
  1502. { check C cdecl para types }
  1503. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkparatype,nil);
  1504. { Adjust alignment to match cdecl or stdcall }
  1505. tprocdef(def).parast.dataalignment:=std_param_align;
  1506. end;
  1507. end;
  1508. pocall_stdcall :
  1509. begin
  1510. include(def.procoptions,po_savestdregs);
  1511. if (def.deftype=procdef) then
  1512. begin
  1513. if not assigned(tprocdef(def).parast) then
  1514. internalerror(200110236);
  1515. { Adjust alignment to match cdecl or stdcall }
  1516. tprocdef(def).parast.dataalignment:=std_param_align;
  1517. end;
  1518. end;
  1519. pocall_safecall :
  1520. begin
  1521. include(def.procoptions,po_savestdregs);
  1522. end;
  1523. pocall_compilerproc :
  1524. begin
  1525. if (not assigned(sym)) or
  1526. (def.deftype<>procdef) then
  1527. internalerror(200110232);
  1528. tprocdef(def).setmangledname(lower(sym.name));
  1529. end;
  1530. pocall_pascal :
  1531. begin
  1532. include(def.procoptions,po_leftright);
  1533. end;
  1534. pocall_register :
  1535. begin
  1536. Message1(parser_w_proc_directive_ignored,'REGISTER');
  1537. end;
  1538. pocall_far16 :
  1539. begin
  1540. { Temporary stub, must be rewritten to support OS/2 far16 }
  1541. Message1(parser_w_proc_directive_ignored,'FAR16');
  1542. end;
  1543. pocall_system :
  1544. begin
  1545. include(def.procoptions,po_clearstack);
  1546. if (not assigned(sym)) or
  1547. (def.deftype<>procdef) then
  1548. internalerror(200110233);
  1549. if not tprocdef(def).has_mangledname then
  1550. tprocdef(def).setmangledname(sym.realname);
  1551. end;
  1552. pocall_palmossyscall :
  1553. begin
  1554. { use popstack and save std registers }
  1555. include(def.procoptions,po_clearstack);
  1556. include(def.procoptions,po_savestdregs);
  1557. if (def.deftype=procdef) then
  1558. begin
  1559. if not assigned(tprocdef(def).parast) then
  1560. internalerror(200110236);
  1561. { Adjust positions of args for cdecl or stdcall }
  1562. tprocdef(def).parast.dataalignment:=std_param_align;
  1563. end;
  1564. end;
  1565. pocall_inline :
  1566. begin
  1567. if not(cs_support_inline in aktmoduleswitches) then
  1568. begin
  1569. Message(parser_e_proc_inline_not_supported);
  1570. def.proccalloption:=pocall_fpccall;
  1571. end;
  1572. end;
  1573. end;
  1574. { add mangledname to external list }
  1575. if (def.deftype=procdef) and
  1576. (po_external in def.procoptions) and
  1577. target_info.DllScanSupported then
  1578. current_module.externals.insert(tExternalsItem.create(tprocdef(def).mangledname));
  1579. end;
  1580. procedure calc_parasymtable_addresses(def:tprocdef);
  1581. var
  1582. lastps,
  1583. highps,ps : tsym;
  1584. st : tsymtable;
  1585. begin
  1586. st:=def.parast;
  1587. if po_leftright in def.procoptions then
  1588. begin
  1589. { pushed in reversed order, left to right }
  1590. highps:=nil;
  1591. lastps:=nil;
  1592. while assigned(st.symindex.first) and (lastps<>tsym(st.symindex.first)) do
  1593. begin
  1594. ps:=tsym(st.symindex.first);
  1595. while assigned(ps.indexnext) and (tsym(ps.indexnext)<>lastps) do
  1596. ps:=tsym(ps.indexnext);
  1597. if (ps.typ=varsym) and
  1598. (copy(ps.name,1,6)<>'hidden') then
  1599. begin
  1600. { Wait with inserting the high value, it needs to be inserted
  1601. after the corresponding parameter }
  1602. if Copy(ps.name,1,4)='high' then
  1603. highps:=ps
  1604. else
  1605. begin
  1606. st.insertvardata(ps);
  1607. { add also the high tree if it was saved }
  1608. if assigned(highps) then
  1609. begin
  1610. st.insertvardata(highps);
  1611. highps:=nil;
  1612. end;
  1613. end;
  1614. end;
  1615. lastps:=ps;
  1616. end;
  1617. if assigned(highps) then
  1618. internalerror(200208257);
  1619. end
  1620. else
  1621. begin
  1622. { pushed in normal order, right to left }
  1623. ps:=tsym(st.symindex.first);
  1624. while assigned(ps) do
  1625. begin
  1626. if ps.typ=varsym then
  1627. st.insertvardata(ps);
  1628. ps:=tsym(ps.indexnext);
  1629. end;
  1630. end;
  1631. end;
  1632. procedure parse_proc_directives(var pdflags:word);
  1633. {
  1634. Parse the procedure directives. It does not matter if procedure directives
  1635. are written using ;procdir; or ['procdir'] syntax.
  1636. }
  1637. var
  1638. res : boolean;
  1639. begin
  1640. while token in [_ID,_LECKKLAMMER] do
  1641. begin
  1642. if try_to_consume(_LECKKLAMMER) then
  1643. begin
  1644. repeat
  1645. parse_proc_direc(pdflags);
  1646. until not try_to_consume(_COMMA);
  1647. consume(_RECKKLAMMER);
  1648. { we always expect at least '[];' }
  1649. res:=true;
  1650. end
  1651. else
  1652. begin
  1653. res:=parse_proc_direc(pdflags);
  1654. end;
  1655. { A procedure directive normally followed by a semicolon, but in
  1656. a const section we should stop when _EQUAL is found }
  1657. if res then
  1658. begin
  1659. if (block_type=bt_const) and
  1660. (token=_EQUAL) then
  1661. break;
  1662. { support procedure proc;stdcall export; in Delphi mode only }
  1663. if not((m_delphi in aktmodeswitches) and
  1664. is_proc_directive(token)) then
  1665. consume(_SEMICOLON);
  1666. end
  1667. else
  1668. break;
  1669. end;
  1670. handle_calling_convention(aktprocsym,aktprocdef);
  1671. { calculate addresses in parasymtable }
  1672. if aktprocdef.deftype=procdef then
  1673. calc_parasymtable_addresses(aktprocdef);
  1674. end;
  1675. procedure parse_var_proc_directives(var sym : tsym);
  1676. var
  1677. pdflags : word;
  1678. oldsym : tprocsym;
  1679. olddef : tprocdef;
  1680. pd : tabstractprocdef;
  1681. begin
  1682. oldsym:=aktprocsym;
  1683. olddef:=aktprocdef;
  1684. pdflags:=pd_procvar;
  1685. { we create a temporary aktprocsym to read the directives }
  1686. aktprocsym:=tprocsym.create(sym.name);
  1687. case sym.typ of
  1688. varsym :
  1689. pd:=tabstractprocdef(tvarsym(sym).vartype.def);
  1690. typedconstsym :
  1691. pd:=tabstractprocdef(ttypedconstsym(sym).typedconsttype.def);
  1692. typesym :
  1693. pd:=tabstractprocdef(ttypesym(sym).restype.def);
  1694. else
  1695. internalerror(994932432);
  1696. end;
  1697. if pd.deftype<>procvardef then
  1698. internalerror(994932433);
  1699. tabstractprocdef(aktprocdef):=pd;
  1700. { names should never be used anyway }
  1701. inc(lexlevel);
  1702. parse_proc_directives(pdflags);
  1703. dec(lexlevel);
  1704. aktprocsym.free;
  1705. aktprocsym:=oldsym;
  1706. aktprocdef:=olddef;
  1707. end;
  1708. procedure parse_object_proc_directives(var sym : tprocsym);
  1709. var
  1710. pdflags : word;
  1711. begin
  1712. pdflags:=pd_object;
  1713. inc(lexlevel);
  1714. parse_proc_directives(pdflags);
  1715. dec(lexlevel);
  1716. if (po_containsself in aktprocdef.procoptions) and
  1717. (([po_msgstr,po_msgint]*aktprocdef.procoptions)=[]) then
  1718. Message(parser_e_self_in_non_message_handler);
  1719. end;
  1720. function proc_add_definition(aprocsym:tprocsym;var aprocdef : tprocdef) : boolean;
  1721. {
  1722. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  1723. forwarddef is found and reused it returns true
  1724. }
  1725. var
  1726. hd : tprocdef;
  1727. ad,fd : tsym;
  1728. forwardfound : boolean;
  1729. i : cardinal;
  1730. begin
  1731. forwardfound:=false;
  1732. { check overloaded functions if the same function already exists }
  1733. for i:=1 to aprocsym.procdef_count do
  1734. begin
  1735. hd:=aprocsym.procdef[i];
  1736. { Skip overloaded definitions that are declared in other
  1737. units }
  1738. if hd.procsym<>aprocsym then
  1739. continue;
  1740. { check the parameters, for delphi/tp it is possible to
  1741. leave the parameters away in the implementation (forwarddef=false).
  1742. But for an overload declared function this is not allowed }
  1743. if { check if empty implementation arguments match is allowed }
  1744. (
  1745. not(m_repeat_forward in aktmodeswitches) and
  1746. not(aprocdef.forwarddef) and
  1747. (aprocdef.maxparacount=0) and
  1748. not(po_overload in hd.procoptions)
  1749. ) or
  1750. { check arguments }
  1751. (
  1752. (compare_paras(aprocdef.para,hd.para,cp_none,false)>=te_equal) and
  1753. { for operators equal_paras is not enough !! }
  1754. ((aprocdef.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1755. equal_defs(hd.rettype.def,aprocdef.rettype.def))
  1756. ) then
  1757. begin
  1758. { Check if we've found the forwarddef, if found then
  1759. we need to update the forward def with the current
  1760. implementation settings }
  1761. if hd.forwarddef then
  1762. begin
  1763. forwardfound:=true;
  1764. { Check if the procedure type and return type are correct,
  1765. also the parameters must match also with the type }
  1766. if (hd.proctypeoption<>aprocdef.proctypeoption) or
  1767. (
  1768. (m_repeat_forward in aktmodeswitches) and
  1769. (not((aprocdef.maxparacount=0) or
  1770. (compare_paras(aprocdef.para,hd.para,cp_all,false)>=te_equal)))
  1771. ) or
  1772. (
  1773. ((m_repeat_forward in aktmodeswitches) or
  1774. not(is_void(aprocdef.rettype.def))) and
  1775. (not equal_defs(hd.rettype.def,aprocdef.rettype.def))) then
  1776. begin
  1777. MessagePos1(aprocdef.fileinfo,parser_e_header_dont_match_forward,
  1778. aprocdef.fullprocname);
  1779. break;
  1780. end;
  1781. { Check if both are declared forward }
  1782. if hd.forwarddef and aprocdef.forwarddef then
  1783. begin
  1784. MessagePos1(aprocdef.fileinfo,parser_e_function_already_declared_public_forward,
  1785. aprocdef.fullprocname);
  1786. end;
  1787. { internconst or internproc only need to be defined once }
  1788. if (hd.proccalloption=pocall_internproc) then
  1789. aprocdef.proccalloption:=hd.proccalloption
  1790. else
  1791. if (aprocdef.proccalloption=pocall_internproc) then
  1792. hd.proccalloption:=aprocdef.proccalloption;
  1793. if (po_internconst in hd.procoptions) then
  1794. include(aprocdef.procoptions,po_internconst)
  1795. else if (po_internconst in aprocdef.procoptions) then
  1796. include(hd.procoptions,po_internconst);
  1797. { Check calling convention }
  1798. if (hd.proccalloption<>aprocdef.proccalloption) then
  1799. begin
  1800. { For delphi check if the current implementation has no proccalloption, then
  1801. take the options from the interface }
  1802. if not(m_delphi in aktmodeswitches) or
  1803. (aprocdef.proccalloption<>pocall_none) then
  1804. MessagePos(aprocdef.fileinfo,parser_e_call_convention_dont_match_forward);
  1805. { restore interface settings }
  1806. aprocdef.proccalloption:=hd.proccalloption;
  1807. if hd.has_mangledname then
  1808. aprocdef.setmangledname(hd.mangledname);
  1809. end;
  1810. { Check manglednames }
  1811. if (m_repeat_forward in aktmodeswitches) or
  1812. aprocdef.haspara then
  1813. begin
  1814. { If mangled names are equal then they have the same amount of arguments }
  1815. { We can check the names of the arguments }
  1816. { both symtables are in the same order from left to right }
  1817. ad:=tsym(hd.parast.symindex.first);
  1818. fd:=tsym(aprocdef.parast.symindex.first);
  1819. repeat
  1820. { skip default parameter constsyms }
  1821. while assigned(ad) and (ad.typ<>varsym) do
  1822. ad:=tsym(ad.indexnext);
  1823. while assigned(fd) and (fd.typ<>varsym) do
  1824. fd:=tsym(fd.indexnext);
  1825. { stop when one of the two lists is at the end }
  1826. if not assigned(ad) or not assigned(fd) then
  1827. break;
  1828. if (ad.name<>fd.name) then
  1829. begin
  1830. MessagePos3(aprocdef.fileinfo,parser_e_header_different_var_names,
  1831. aprocsym.name,ad.name,fd.name);
  1832. break;
  1833. end;
  1834. ad:=tsym(ad.indexnext);
  1835. fd:=tsym(fd.indexnext);
  1836. until false;
  1837. if assigned(ad) or assigned(fd) then
  1838. internalerror(200204178);
  1839. end;
  1840. { Everything is checked, now we can update the forward declaration
  1841. with the new data from the implementation }
  1842. hd.forwarddef:=aprocdef.forwarddef;
  1843. hd.hasforward:=true;
  1844. hd.parast.address_fixup:=aprocdef.parast.address_fixup;
  1845. hd.procoptions:=hd.procoptions+aprocdef.procoptions;
  1846. if hd.extnumber=65535 then
  1847. hd.extnumber:=aprocdef.extnumber;
  1848. while not aprocdef.aliasnames.empty do
  1849. hd.aliasnames.insert(aprocdef.aliasnames.getfirst);
  1850. { update mangledname if the implementation has a fixed mangledname set }
  1851. if aprocdef.has_mangledname then
  1852. begin
  1853. { rename also asmsymbol first, because the name can already be used }
  1854. objectlibrary.renameasmsymbol(hd.mangledname,aprocdef.mangledname);
  1855. hd.setmangledname(aprocdef.mangledname);
  1856. end;
  1857. { for compilerproc defines we need to rename and update the
  1858. symbolname to lowercase }
  1859. if (aprocdef.proccalloption=pocall_compilerproc) then
  1860. begin
  1861. { rename to lowercase so users can't access it }
  1862. aprocsym.owner.rename(aprocsym.name,lower(aprocsym.name));
  1863. { also update the realname that is stored in the ppu }
  1864. stringdispose(aprocsym._realname);
  1865. aprocsym._realname:=stringdup('$'+aprocsym.name);
  1866. { the mangeled name is already changed by the pd_compilerproc }
  1867. { handler. It must be done immediately because if we have a }
  1868. { call to a compilerproc before it's implementation is }
  1869. { encountered, it must already use the new mangled name (JM) }
  1870. end;
  1871. { return the forwarddef }
  1872. aprocdef:=hd;
  1873. end
  1874. else
  1875. begin
  1876. { abstract methods aren't forward defined, but this }
  1877. { needs another error message }
  1878. if (po_abstractmethod in hd.procoptions) then
  1879. MessagePos(aprocdef.fileinfo,parser_e_abstract_no_definition)
  1880. else
  1881. MessagePos(aprocdef.fileinfo,parser_e_overloaded_have_same_parameters);
  1882. end;
  1883. { we found one proc with the same arguments, there are no others
  1884. so we can stop }
  1885. break;
  1886. end;
  1887. { check for allowing overload directive }
  1888. if not(m_fpc in aktmodeswitches) then
  1889. begin
  1890. { overload directive turns on overloading }
  1891. if ((po_overload in aprocdef.procoptions) or
  1892. (po_overload in hd.procoptions)) then
  1893. begin
  1894. { check if all procs have overloading, but not if the proc was
  1895. already declared forward, then the check is already done }
  1896. if not(hd.hasforward or
  1897. (aprocdef.forwarddef<>hd.forwarddef) or
  1898. ((po_overload in aprocdef.procoptions) and
  1899. (po_overload in hd.procoptions))) then
  1900. begin
  1901. MessagePos1(aprocdef.fileinfo,parser_e_no_overload_for_all_procs,aprocsym.realname);
  1902. break;
  1903. end;
  1904. end
  1905. else
  1906. begin
  1907. if not(hd.forwarddef) then
  1908. begin
  1909. MessagePos(aprocdef.fileinfo,parser_e_procedure_overloading_is_off);
  1910. break;
  1911. end;
  1912. end;
  1913. end; { equal arguments }
  1914. end;
  1915. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  1916. list }
  1917. if not forwardfound then
  1918. begin
  1919. aprocsym.addprocdef(aprocdef);
  1920. { add overloadnumber for unique naming, the overloadcount is
  1921. counted per module and 0 for the first procedure }
  1922. aprocdef.overloadnumber:=aprocsym.overloadcount;
  1923. inc(aprocsym.overloadcount);
  1924. end;
  1925. { insert otsym only in the right symtable }
  1926. if ((procinfo.flags and pi_operator)<>0) and
  1927. assigned(otsym) then
  1928. begin
  1929. if not parse_only then
  1930. begin
  1931. if paramanager.ret_in_param(aprocdef.rettype.def,aprocdef.proccalloption) then
  1932. begin
  1933. aprocdef.parast.insert(otsym);
  1934. { this allows to read the funcretoffset }
  1935. otsym.address:=-4;
  1936. otsym.varspez:=vs_var;
  1937. end
  1938. else
  1939. begin
  1940. if not assigned(aprocdef.localst) then
  1941. aprocdef.insert_localst;
  1942. aprocdef.localst.insert(otsym);
  1943. aprocdef.localst.insertvardata(otsym);
  1944. end;
  1945. end
  1946. else
  1947. begin
  1948. { this is not required anymore }
  1949. otsym.free;
  1950. otsym:=nil;
  1951. end;
  1952. end;
  1953. paramanager.create_param_loc_info(aprocdef);
  1954. proc_add_definition:=forwardfound;
  1955. end;
  1956. end.
  1957. {
  1958. $Log$
  1959. Revision 1.90 2002-12-17 22:19:33 peter
  1960. * fixed pushing of records>8 bytes with stdcall
  1961. * simplified hightree loading
  1962. Revision 1.89 2002/12/15 21:07:30 peter
  1963. * don't allow external in object declarations
  1964. Revision 1.88 2002/12/15 19:34:31 florian
  1965. + some front end stuff for vs_hidden added
  1966. Revision 1.87 2002/12/07 14:27:07 carl
  1967. * 3% memory optimization
  1968. * changed some types
  1969. + added type checking with different size for call node and for
  1970. parameters
  1971. Revision 1.86 2002/12/06 17:51:10 peter
  1972. * merged cdecl and array fixes
  1973. Revision 1.85 2002/12/01 22:06:14 carl
  1974. * cleanup of error messages
  1975. Revision 1.84 2002/11/29 22:31:19 carl
  1976. + unimplemented hint directive added
  1977. * hint directive parsing implemented
  1978. * warning on these directives
  1979. Revision 1.83 2002/11/27 02:35:28 peter
  1980. * fixed typo in method comparing
  1981. Revision 1.82 2002/11/25 17:43:21 peter
  1982. * splitted defbase in defutil,symutil,defcmp
  1983. * merged isconvertable and is_equal into compare_defs(_ext)
  1984. * made operator search faster by walking the list only once
  1985. Revision 1.81 2002/11/18 17:31:58 peter
  1986. * pass proccalloption to ret_in_xxx and push_xxx functions
  1987. Revision 1.80 2002/11/17 16:31:56 carl
  1988. * memory optimization (3-4%) : cleanup of tai fields,
  1989. cleanup of tdef and tsym fields.
  1990. * make it work for m68k
  1991. Revision 1.79 2002/11/16 14:20:50 peter
  1992. * fix infinite loop in pd_inline
  1993. Revision 1.78 2002/11/15 01:58:53 peter
  1994. * merged changes from 1.0.7 up to 04-11
  1995. - -V option for generating bug report tracing
  1996. - more tracing for option parsing
  1997. - errors for cdecl and high()
  1998. - win32 import stabs
  1999. - win32 records<=8 are returned in eax:edx (turned off by default)
  2000. - heaptrc update
  2001. - more info for temp management in .s file with EXTDEBUG
  2002. Revision 1.77 2002/10/06 15:09:12 peter
  2003. * variant:=nil supported
  2004. Revision 1.76 2002/09/27 21:13:29 carl
  2005. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  2006. Revision 1.75 2002/09/16 14:11:13 peter
  2007. * add argument to equal_paras() to support default values or not
  2008. Revision 1.74 2002/09/10 16:27:28 peter
  2009. * don't insert parast in symtablestack, because typesyms should not be
  2010. searched in the the parast
  2011. Revision 1.73 2002/09/09 19:39:07 peter
  2012. * check return type for forwarddefs also not delphi mode when
  2013. the type is not void
  2014. Revision 1.72 2002/09/09 17:34:15 peter
  2015. * tdicationary.replace added to replace and item in a dictionary. This
  2016. is only allowed for the same name
  2017. * varsyms are inserted in symtable before the types are parsed. This
  2018. fixes the long standing "var longint : longint" bug
  2019. - consume_idlist and idstringlist removed. The loops are inserted
  2020. at the callers place and uses the symtable for duplicate id checking
  2021. Revision 1.71 2002/09/07 15:25:06 peter
  2022. * old logs removed and tabs fixed
  2023. Revision 1.70 2002/09/03 16:26:27 daniel
  2024. * Make Tprocdef.defs protected
  2025. Revision 1.69 2002/09/01 12:11:33 peter
  2026. * calc param_offset after parameters are read, because the calculation
  2027. depends on po_containself
  2028. Revision 1.68 2002/08/25 19:25:20 peter
  2029. * sym.insert_in_data removed
  2030. * symtable.insertvardata/insertconstdata added
  2031. * removed insert_in_data call from symtable.insert, it needs to be
  2032. called separatly. This allows to deref the address calculation
  2033. * procedures now calculate the parast addresses after the procedure
  2034. directives are parsed. This fixes the cdecl parast problem
  2035. * push_addr_param has an extra argument that specifies if cdecl is used
  2036. or not
  2037. Revision 1.67 2002/08/25 11:33:06 peter
  2038. * also check the paratypes when a forward was found
  2039. Revision 1.66 2002/08/19 19:36:44 peter
  2040. * More fixes for cross unit inlining, all tnodes are now implemented
  2041. * Moved pocall_internconst to po_internconst because it is not a
  2042. calling type at all and it conflicted when inlining of these small
  2043. functions was requested
  2044. Revision 1.65 2002/08/18 20:06:24 peter
  2045. * inlining is now also allowed in interface
  2046. * renamed write/load to ppuwrite/ppuload
  2047. * tnode storing in ppu
  2048. * nld,ncon,nbas are already updated for storing in ppu
  2049. Revision 1.64 2002/08/17 09:23:39 florian
  2050. * first part of procinfo rewrite
  2051. Revision 1.63 2002/08/11 14:32:27 peter
  2052. * renamed current_library to objectlibrary
  2053. Revision 1.62 2002/08/11 13:24:12 peter
  2054. * saving of asmsymbols in ppu supported
  2055. * asmsymbollist global is removed and moved into a new class
  2056. tasmlibrarydata that will hold the info of a .a file which
  2057. corresponds with a single module. Added librarydata to tmodule
  2058. to keep the library info stored for the module. In the future the
  2059. objectfiles will also be stored to the tasmlibrarydata class
  2060. * all getlabel/newasmsymbol and friends are moved to the new class
  2061. Revision 1.61 2002/07/26 21:15:40 florian
  2062. * rewrote the system handling
  2063. Revision 1.60 2002/07/20 11:57:55 florian
  2064. * types.pas renamed to defbase.pas because D6 contains a types
  2065. unit so this would conflicts if D6 programms are compiled
  2066. + Willamette/SSE2 instructions to assembler added
  2067. Revision 1.59 2002/07/11 14:41:28 florian
  2068. * start of the new generic parameter handling
  2069. Revision 1.58 2002/07/01 18:46:25 peter
  2070. * internal linker
  2071. * reorganized aasm layer
  2072. Revision 1.57 2002/05/18 13:34:12 peter
  2073. * readded missing revisions
  2074. Revision 1.56 2002/05/16 19:46:42 carl
  2075. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2076. + try to fix temp allocation (still in ifdef)
  2077. + generic constructor calls
  2078. + start of tassembler / tmodulebase class cleanup
  2079. Revision 1.54 2002/05/12 16:53:08 peter
  2080. * moved entry and exitcode to ncgutil and cgobj
  2081. * foreach gets extra argument for passing local data to the
  2082. iterator function
  2083. * -CR checks also class typecasts at runtime by changing them
  2084. into as
  2085. * fixed compiler to cycle with the -CR option
  2086. * fixed stabs with elf writer, finally the global variables can
  2087. be watched
  2088. * removed a lot of routines from cga unit and replaced them by
  2089. calls to cgobj
  2090. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2091. u32bit then the other is typecasted also to u32bit without giving
  2092. a rangecheck warning/error.
  2093. * fixed pascal calling method with reversing also the high tree in
  2094. the parast, detected by tcalcst3 test
  2095. Revision 1.53 2002/04/21 19:02:04 peter
  2096. * removed newn and disposen nodes, the code is now directly
  2097. inlined from pexpr
  2098. * -an option that will write the secondpass nodes to the .s file, this
  2099. requires EXTDEBUG define to actually write the info
  2100. * fixed various internal errors and crashes due recent code changes
  2101. Revision 1.52 2002/04/20 21:32:24 carl
  2102. + generic FPC_CHECKPOINTER
  2103. + first parameter offset in stack now portable
  2104. * rename some constants
  2105. + move some cpu stuff to other units
  2106. - remove unused constents
  2107. * fix stacksize for some targets
  2108. * fix generic size problems which depend now on EXTEND_SIZE constant
  2109. Revision 1.51 2002/04/20 15:27:05 carl
  2110. - remove ifdef i386 define
  2111. Revision 1.50 2002/04/19 15:46:02 peter
  2112. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  2113. in most cases and not written to the ppu
  2114. * add mangeledname_prefix() routine to generate the prefix of
  2115. manglednames depending on the current procedure, object and module
  2116. * removed static procprefix since the mangledname is now build only
  2117. on demand from tprocdef.mangledname
  2118. }