pdecsub.pas 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used in implementation section }
  26. pd_interface, { directive can be used in interface section }
  27. pd_object, { directive can be used with object declaration }
  28. pd_record, { directive can be used with record declaration }
  29. pd_procvar, { directive can be used with procvar declaration }
  30. pd_notobject, { directive can not be used with object declaration }
  31. pd_notrecord, { directive can not be used with record declaration }
  32. pd_notobjintf, { directive can not be used with interface declaration }
  33. pd_notprocvar, { directive can not be used with procvar declaration }
  34. pd_dispinterface,{ directive can be used with dispinterface methods }
  35. pd_cppobject, { directive can be used with cppclass }
  36. pd_objcclass, { directive can be used with objcclass }
  37. pd_objcprot, { directive can be used with objcprotocol }
  38. pd_nothelper, { directive can not be used with record/class helper declaration }
  39. pd_javaclass, { directive can be used with Java class }
  40. pd_intfjava { directive can be used with Java interface }
  41. );
  42. tpdflags=set of tpdflag;
  43. // flags of handle_calling_convention routine
  44. thccflag=(
  45. hcc_check, // perform checks and outup errors if found
  46. hcc_insert_hidden_paras // insert hidden parameters
  47. );
  48. thccflags=set of thccflag;
  49. const
  50. hcc_all=[hcc_check,hcc_insert_hidden_paras];
  51. function check_proc_directive(isprocvar:boolean):boolean;
  52. function proc_add_definition(var currpd:tprocdef):boolean;
  53. function proc_get_importname(pd:tprocdef):string;
  54. procedure proc_set_mangledname(pd:tprocdef);
  55. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  56. procedure parse_parameter_dec(pd:tabstractprocdef);
  57. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  58. procedure parse_var_proc_directives(sym:tsym);
  59. procedure parse_object_proc_directives(pd:tabstractprocdef);
  60. procedure parse_record_proc_directives(pd:tabstractprocdef);
  61. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  62. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  63. { parse a record method declaration (not a (class) constructor/destructor) }
  64. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  65. procedure insert_record_hidden_paras(astruct: trecorddef);
  66. { helper functions - they insert nested objects hierarcy to the symtablestack
  67. with object hierarchy
  68. }
  69. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  70. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  71. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  72. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  73. implementation
  74. uses
  75. SysUtils,
  76. { common }
  77. cutils,cclasses,
  78. { global }
  79. globtype,globals,verbose,constexp,
  80. systems,
  81. cpuinfo,
  82. { symtable }
  83. symbase,symcpu,symtable,defutil,defcmp,
  84. { parameter handling }
  85. paramgr,cpupara,
  86. { pass 1 }
  87. fmodule,node,htypechk,ncon,ppu,
  88. objcutil,
  89. { parser }
  90. scanner,
  91. pbase,pexpr,ptype,pdecl,pparautl,pgenutil
  92. {$ifdef jvm}
  93. ,pjvm
  94. {$endif}
  95. ;
  96. const
  97. { Please leave this here, this module should NOT use
  98. these variables.
  99. Declaring it as string here results in an error when compiling (PFV) }
  100. current_procinfo = 'error';
  101. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  102. var
  103. _class,hp : tobjectdef;
  104. begin
  105. if obj.typ=recorddef then
  106. begin
  107. symtablestack.push(obj.symtable);
  108. result:=1;
  109. exit;
  110. end;
  111. result:=0;
  112. { insert class hierarchy in the reverse order }
  113. hp:=nil;
  114. repeat
  115. _class:=tobjectdef(obj);
  116. while _class.childof<>hp do
  117. _class:=_class.childof;
  118. hp:=_class;
  119. symtablestack.push(_class.symtable);
  120. inc(result);
  121. until hp=obj;
  122. end;
  123. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  124. begin
  125. result:=0;
  126. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  127. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  128. inc(result,push_child_hierarcy(obj));
  129. end;
  130. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  131. var
  132. _class : tobjectdef;
  133. begin
  134. if obj.typ=recorddef then
  135. begin
  136. symtablestack.pop(obj.symtable);
  137. result:=1;
  138. exit;
  139. end;
  140. result:=0;
  141. _class:=tobjectdef(obj);
  142. while assigned(_class) do
  143. begin
  144. symtablestack.pop(_class.symtable);
  145. _class:=_class.childof;
  146. inc(result);
  147. end;
  148. end;
  149. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  150. begin
  151. result:=pop_child_hierarchy(obj);
  152. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  153. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  154. end;
  155. procedure check_msg_para(p:TObject;arg:pointer);
  156. begin
  157. if (tsym(p).typ<>paravarsym) then
  158. exit;
  159. with tparavarsym(p) do
  160. begin
  161. { Count parameters }
  162. if (paranr>=10) then
  163. inc(plongint(arg)^);
  164. { First parameter must be var }
  165. if (paranr=10) and
  166. (varspez<>vs_var) then
  167. MessagePos(fileinfo,parser_e_ill_msg_param);
  168. end;
  169. end;
  170. procedure set_addr_param_regable(p:TObject;arg:pointer);
  171. begin
  172. if (tsym(p).typ<>paravarsym) then
  173. exit;
  174. with tparavarsym(p) do
  175. begin
  176. if (not needs_finalization) and
  177. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  178. varregable:=vr_addr;
  179. end;
  180. end;
  181. procedure parse_parameter_dec(pd:tabstractprocdef);
  182. {
  183. handle_procvar needs the same changes
  184. }
  185. type
  186. tppv = (pv_none,pv_proc,pv_func);
  187. var
  188. sc : TFPObjectList;
  189. hdef : tdef;
  190. arrayelementdef : tdef;
  191. vs : tparavarsym;
  192. i : longint;
  193. srsym : tsym;
  194. pv : tprocvardef;
  195. varspez : Tvarspez;
  196. defaultvalue : tconstsym;
  197. defaultrequired : boolean;
  198. old_block_type : tblock_type;
  199. currparast : tparasymtable;
  200. parseprocvar : tppv;
  201. locationstr : string;
  202. paranr : integer;
  203. dummytype : ttypesym;
  204. explicit_paraloc,
  205. need_array,
  206. is_univ: boolean;
  207. stoptions : TSingleTypeOptions;
  208. procedure handle_default_para_value;
  209. var
  210. convpd : tprocdef;
  211. doconv : tconverttype;
  212. nodetype : tnodetype;
  213. bt : tblock_type;
  214. begin
  215. { only allowed for types that can be represented by a
  216. constant expression }
  217. if try_to_consume(_EQ) then
  218. begin
  219. if (hdef.typ in [recorddef,variantdef,filedef,formaldef]) or
  220. is_object(hdef) or
  221. ((hdef.typ=arraydef) and
  222. not is_dynamic_array(hdef)) then
  223. Message1(type_e_invalid_default_value,FullTypeName(hdef,nil));
  224. vs:=tparavarsym(sc[0]);
  225. if sc.count>1 then
  226. Message(parser_e_default_value_only_one_para);
  227. bt:=block_type;
  228. block_type:=bt_const;
  229. { prefix 'def' to the parameter name }
  230. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo,nodetype);
  231. block_type:=bt;
  232. if assigned(defaultvalue) then
  233. begin
  234. include(defaultvalue.symoptions,sp_internal);
  235. pd.parast.insert(defaultvalue);
  236. { check whether the default value is of the correct
  237. type }
  238. if compare_defs_ext(defaultvalue.constdef,hdef,nodetype,doconv,convpd,[])<=te_convert_operator then
  239. MessagePos2(defaultvalue.fileinfo,type_e_incompatible_types,FullTypeName(defaultvalue.constdef,hdef),FullTypeName(hdef,defaultvalue.constdef));
  240. end;
  241. defaultrequired:=true;
  242. end
  243. else
  244. begin
  245. if defaultrequired then
  246. Message1(parser_e_default_value_expected_for_para,vs.name);
  247. end;
  248. end;
  249. begin
  250. old_block_type:=block_type;
  251. explicit_paraloc:=false;
  252. consume(_LKLAMMER);
  253. { Delphi/Kylix supports nonsense like }
  254. { procedure p(); }
  255. if try_to_consume(_RKLAMMER) and
  256. not(m_tp7 in current_settings.modeswitches) then
  257. exit;
  258. { parsing a proc or procvar ? }
  259. currparast:=tparasymtable(pd.parast);
  260. { reset }
  261. sc:=TFPObjectList.create(false);
  262. defaultrequired:=false;
  263. paranr:=0;
  264. block_type:=bt_var;
  265. is_univ:=false;
  266. repeat
  267. parseprocvar:=pv_none;
  268. if try_to_consume(_VAR) then
  269. varspez:=vs_var
  270. else
  271. if try_to_consume(_CONST) then
  272. varspez:=vs_const
  273. else
  274. if (m_out in current_settings.modeswitches) and
  275. try_to_consume(_OUT) then
  276. varspez:=vs_out
  277. else
  278. if try_to_consume(_CONSTREF) then
  279. varspez:=vs_constref
  280. else
  281. if (m_mac in current_settings.modeswitches) and
  282. try_to_consume(_POINTPOINTPOINT) then
  283. begin
  284. include(pd.procoptions,po_varargs);
  285. break;
  286. end
  287. else
  288. if (m_nested_procvars in current_settings.modeswitches) and
  289. try_to_consume(_PROCEDURE) then
  290. begin
  291. parseprocvar:=pv_proc;
  292. varspez:=vs_const;
  293. end
  294. else
  295. if (m_nested_procvars in current_settings.modeswitches) and
  296. try_to_consume(_FUNCTION) then
  297. begin
  298. parseprocvar:=pv_func;
  299. varspez:=vs_const;
  300. end
  301. else
  302. varspez:=vs_value;
  303. defaultvalue:=nil;
  304. hdef:=nil;
  305. { read identifiers and insert with error type }
  306. sc.clear;
  307. repeat
  308. inc(paranr);
  309. vs:=cparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  310. currparast.insert(vs);
  311. if assigned(vs.owner) then
  312. sc.add(vs)
  313. else
  314. vs.free;
  315. consume(_ID);
  316. until not try_to_consume(_COMMA);
  317. locationstr:='';
  318. { macpas anonymous procvar }
  319. if parseprocvar<>pv_none then
  320. begin
  321. { inline procvar definitions are always nested procvars }
  322. pv:=cprocvardef.create(normal_function_level+1);
  323. if token=_LKLAMMER then
  324. parse_parameter_dec(pv);
  325. if parseprocvar=pv_func then
  326. begin
  327. block_type:=bt_var_type;
  328. consume(_COLON);
  329. single_type(pv.returndef,[]);
  330. block_type:=bt_var;
  331. end;
  332. hdef:=pv;
  333. { possible proc directives }
  334. if check_proc_directive(true) then
  335. begin
  336. dummytype:=ctypesym.create('unnamed',hdef);
  337. parse_var_proc_directives(tsym(dummytype));
  338. dummytype.typedef:=nil;
  339. hdef.typesym:=nil;
  340. dummytype.free;
  341. end;
  342. { Add implicit hidden parameters and function result }
  343. handle_calling_convention(pv);
  344. {$ifdef jvm}
  345. { anonymous -> no name }
  346. jvm_create_procvar_class('',pv);
  347. {$endif}
  348. end
  349. else
  350. { read type declaration, force reading for value paras }
  351. if (token=_COLON) or (varspez=vs_value) then
  352. begin
  353. consume(_COLON);
  354. { check for an open array }
  355. need_array:=false;
  356. { bitpacked open array are not yet supported }
  357. if (token=_PACKED) and
  358. not(cs_bitpacking in current_settings.localswitches) then
  359. begin
  360. consume(_PACKED);
  361. need_array:=true;
  362. end;
  363. if (token=_ARRAY) or
  364. need_array then
  365. begin
  366. consume(_ARRAY);
  367. consume(_OF);
  368. { define range and type of range }
  369. hdef:=carraydef.create(0,-1,ptrsinttype);
  370. { array of const ? }
  371. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  372. begin
  373. consume(_CONST);
  374. srsym:=search_system_type('TVARREC');
  375. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  376. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  377. end
  378. else
  379. begin
  380. { define field type }
  381. if m_delphi in current_settings.modeswitches then
  382. stoptions:=[stoAllowSpecialization]
  383. else
  384. stoptions:=[];
  385. single_type(arrayelementdef,stoptions);
  386. tarraydef(hdef).elementdef:=arrayelementdef;
  387. end;
  388. end
  389. else
  390. begin
  391. if (m_mac in current_settings.modeswitches) then
  392. is_univ:=try_to_consume(_UNIV);
  393. if try_to_consume(_TYPE) then
  394. hdef:=ctypedformaltype
  395. else
  396. begin
  397. block_type:=bt_var_type;
  398. single_type(hdef,[stoAllowSpecialization]);
  399. block_type:=bt_var;
  400. end;
  401. { open string ? }
  402. if is_shortstring(hdef) then
  403. begin
  404. case varspez of
  405. vs_var,vs_out:
  406. begin
  407. { not 100% Delphi-compatible: type xstr=string[255] cannot
  408. become an openstring there, while here it can }
  409. if (cs_openstring in current_settings.moduleswitches) and
  410. (tstringdef(hdef).len=255) then
  411. hdef:=openshortstringtype
  412. end;
  413. vs_value:
  414. begin
  415. { value "openstring" parameters don't make sense (the
  416. original string can never be modified, so there's no
  417. use in passing its original length), so change these
  418. into regular shortstring parameters (seems to be what
  419. Delphi also does) }
  420. if is_open_string(hdef) then
  421. hdef:=cshortstringtype;
  422. end;
  423. end;
  424. end;
  425. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  426. begin
  427. if (idtoken=_LOCATION) then
  428. begin
  429. consume(_LOCATION);
  430. locationstr:=cstringpattern;
  431. consume(_CSTRING);
  432. end
  433. else
  434. begin
  435. if explicit_paraloc then
  436. Message(parser_e_paraloc_all_paras);
  437. locationstr:='';
  438. end;
  439. end
  440. else
  441. locationstr:='';
  442. { default parameter }
  443. if (m_default_para in current_settings.modeswitches) then
  444. handle_default_para_value;
  445. end;
  446. end
  447. else
  448. hdef:=cformaltype;
  449. { File types are only allowed for var and out parameters }
  450. if (hdef.typ=filedef) and
  451. not(varspez in [vs_out,vs_var]) then
  452. CGMessage(cg_e_file_must_call_by_reference);
  453. { Dispinterfaces are restricted to using only automatable types }
  454. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  455. not is_automatable(hdef) then
  456. Message1(type_e_not_automatable,hdef.typename);
  457. { univ cannot be used with types whose size is not known at compile
  458. time }
  459. if is_univ and
  460. not is_valid_univ_para_type(hdef) then
  461. Message1(parser_e_invalid_univ_para,hdef.typename);
  462. for i:=0 to sc.count-1 do
  463. begin
  464. vs:=tparavarsym(sc[i]);
  465. vs.univpara:=is_univ;
  466. { update varsym }
  467. vs.vardef:=hdef;
  468. vs.defaultconstsym:=defaultvalue;
  469. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  470. begin
  471. if locationstr<>'' then
  472. begin
  473. if sc.count>1 then
  474. Message(parser_e_paraloc_only_one_para);
  475. if (paranr>1) and not(explicit_paraloc) then
  476. Message(parser_e_paraloc_all_paras);
  477. explicit_paraloc:=true;
  478. include(vs.varoptions,vo_has_explicit_paraloc);
  479. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  480. message(parser_e_illegal_explicit_paraloc);
  481. end
  482. else
  483. if explicit_paraloc then
  484. Message(parser_e_paraloc_all_paras);
  485. end;
  486. end;
  487. until not try_to_consume(_SEMICOLON);
  488. if explicit_paraloc then
  489. begin
  490. pd.has_paraloc_info:=callerside;
  491. include(pd.procoptions,po_explicitparaloc);
  492. end;
  493. { remove parasymtable from stack }
  494. sc.free;
  495. { reset object options }
  496. block_type:=old_block_type;
  497. consume(_RKLAMMER);
  498. end;
  499. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  500. var
  501. hs : string;
  502. orgsp,sp : TIDString;
  503. srsym : tsym;
  504. checkstack : psymtablestackitem;
  505. procstartfilepos : tfileposinfo;
  506. i,
  507. index : longint;
  508. found,
  509. searchagain : boolean;
  510. st,
  511. genericst: TSymtable;
  512. aprocsym : tprocsym;
  513. popclass : integer;
  514. ImplIntf : TImplementedInterface;
  515. old_parse_generic : boolean;
  516. old_current_structdef: tabstractrecorddef;
  517. old_current_genericdef,
  518. old_current_specializedef: tstoreddef;
  519. lasttoken,lastidtoken: ttoken;
  520. procedure parse_operator_name;
  521. begin
  522. if (lasttoken in [first_overloaded..last_overloaded]) then
  523. begin
  524. optoken:=token;
  525. end
  526. else
  527. begin
  528. case lasttoken of
  529. _CARET:
  530. Message1(parser_e_overload_operator_failed,'**');
  531. _ID:
  532. case lastidtoken of
  533. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  534. _EXPLICIT:optoken:=_OP_EXPLICIT;
  535. _INC:optoken:=_OP_INC;
  536. _DEC:optoken:=_OP_DEC;
  537. else
  538. if (m_delphi in current_settings.modeswitches) then
  539. case lastidtoken of
  540. _IMPLICIT:optoken:=_ASSIGNMENT;
  541. _NEGATIVE:optoken:=_MINUS;
  542. _POSITIVE:optoken:=_PLUS;
  543. _LOGICALNOT:optoken:=_OP_NOT;
  544. _IN:optoken:=_OP_IN;
  545. _EQUAL:optoken:=_EQ;
  546. _NOTEQUAL:optoken:=_NE;
  547. _GREATERTHAN:optoken:=_GT;
  548. _GREATERTHANOREQUAL:optoken:=_GTE;
  549. _LESSTHAN:optoken:=_LT;
  550. _LESSTHANOREQUAL:optoken:=_LTE;
  551. _ADD:optoken:=_PLUS;
  552. _SUBTRACT:optoken:=_MINUS;
  553. _MULTIPLY:optoken:=_STAR;
  554. _DIVIDE:optoken:=_SLASH;
  555. _INTDIVIDE:optoken:=_OP_DIV;
  556. _MODULUS:optoken:=_OP_MOD;
  557. _LEFTSHIFT:optoken:=_OP_SHL;
  558. _RIGHTSHIFT:optoken:=_OP_SHR;
  559. _LOGICALAND:optoken:=_OP_AND;
  560. _LOGICALOR:optoken:=_OP_OR;
  561. _LOGICALXOR:optoken:=_OP_XOR;
  562. _BITWISEAND:optoken:=_OP_AND;
  563. _BITWISEOR:optoken:=_OP_OR;
  564. _BITWISEXOR:optoken:=_OP_XOR;
  565. else
  566. Message1(parser_e_overload_operator_failed,'');
  567. end
  568. else
  569. Message1(parser_e_overload_operator_failed,'');
  570. end
  571. else
  572. Message1(parser_e_overload_operator_failed,'');
  573. end;
  574. end;
  575. sp:=overloaded_names[optoken];
  576. orgsp:=sp;
  577. end;
  578. procedure consume_proc_name;
  579. begin
  580. lasttoken:=token;
  581. lastidtoken:=idtoken;
  582. if potype=potype_operator then
  583. optoken:=NOTOKEN;
  584. if (potype=potype_operator) and (token<>_ID) then
  585. begin
  586. parse_operator_name;
  587. consume(token);
  588. end
  589. else
  590. begin
  591. sp:=pattern;
  592. orgsp:=orgpattern;
  593. consume(_ID);
  594. end;
  595. end;
  596. function search_object_name(sp:TIDString;gen_error:boolean):tsym;
  597. var
  598. storepos:tfileposinfo;
  599. srsymtable:TSymtable;
  600. begin
  601. storepos:=current_tokenpos;
  602. current_tokenpos:=procstartfilepos;
  603. searchsym(sp,result,srsymtable);
  604. if not assigned(result) then
  605. begin
  606. if gen_error then
  607. identifier_not_found(orgsp);
  608. result:=generrorsym;
  609. end;
  610. current_tokenpos:=storepos;
  611. end;
  612. function consume_generic_type_parameter:boolean;
  613. var
  614. idx : integer;
  615. genparalistdecl : TFPHashList;
  616. genname : tidstring;
  617. s : shortstring;
  618. begin
  619. result:=not assigned(astruct)and
  620. (m_delphi in current_settings.modeswitches)and
  621. (token in [_LT,_LSHARPBRACKET]);
  622. if result then
  623. begin
  624. consume(token);
  625. { parse all parameters first so we can check whether we have
  626. the correct generic def available }
  627. genparalistdecl:=TFPHashList.Create;
  628. { start with 1, so Find can return Nil (= 0) }
  629. idx:=1;
  630. repeat
  631. if token=_ID then
  632. begin
  633. genparalistdecl.Add(pattern, Pointer(PtrInt(idx)));
  634. consume(_ID);
  635. inc(idx);
  636. end
  637. else
  638. begin
  639. message2(scan_f_syn_expected,arraytokeninfo[_ID].str,arraytokeninfo[token].str);
  640. if token<>_COMMA then
  641. consume(token);
  642. end;
  643. until not try_to_consume(_COMMA);
  644. if not try_to_consume(_GT) then
  645. consume(_RSHARPBRACKET);
  646. s:='';
  647. str(genparalistdecl.count,s);
  648. genname:=sp+'$'+s;
  649. genparalistdecl.free;
  650. srsym:=search_object_name(genname,false);
  651. if not assigned(srsym) then
  652. begin
  653. { TODO : print a nicer typename that contains the parsed
  654. generic types }
  655. Message1(type_e_generic_declaration_does_not_match,genname);
  656. srsym:=nil;
  657. exit;
  658. end
  659. end
  660. else
  661. srsym:=nil;
  662. end;
  663. procedure consume_generic_interface;
  664. var
  665. genparalist : tfpobjectlist;
  666. prettyname,
  667. specializename : ansistring;
  668. genname,
  669. ugenname : tidstring;
  670. gencount : string;
  671. begin
  672. consume(_LSHARPBRACKET);
  673. genparalist:=tfpobjectlist.create(false);
  674. if not parse_generic_specialization_types(genparalist,nil,prettyname,specializename) then
  675. srsym:=generrorsym
  676. else
  677. begin
  678. str(genparalist.count,gencount);
  679. genname:=sp+'$'+gencount;
  680. genname:=generate_generic_name(genname,specializename);
  681. ugenname:=upper(genname);
  682. srsym:=search_object_name(ugenname,false);
  683. if not assigned(srsym) then
  684. begin
  685. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  686. srsym:=nil;
  687. exit;
  688. end;
  689. end;
  690. genparalist.free;
  691. consume(_RSHARPBRACKET);
  692. end;
  693. begin
  694. sp:='';
  695. orgsp:='';
  696. { Save the position where this procedure really starts }
  697. procstartfilepos:=current_tokenpos;
  698. old_parse_generic:=parse_generic;
  699. result:=false;
  700. pd:=nil;
  701. aprocsym:=nil;
  702. srsym:=nil;
  703. consume_proc_name;
  704. { examine interface map: function/procedure iname.functionname=locfuncname }
  705. if assigned(astruct) and
  706. (astruct.typ=objectdef) and
  707. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  708. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  709. (
  710. (token = _POINT) or
  711. (token = _LSHARPBRACKET)
  712. ) then
  713. begin
  714. if token = _POINT then
  715. begin
  716. consume(_POINT);
  717. srsym:=search_object_name(sp,true);
  718. end
  719. else
  720. begin
  721. consume_generic_interface;
  722. consume(_POINT);
  723. { srsym is now either an interface def or generrordef }
  724. end;
  725. { qualifier is interface? }
  726. ImplIntf:=nil;
  727. if (srsym.typ=typesym) and
  728. (ttypesym(srsym).typedef.typ=objectdef) then
  729. ImplIntf:=find_implemented_interface(tobjectdef(astruct),tobjectdef(ttypesym(srsym).typedef));
  730. if ImplIntf=nil then
  731. Message(parser_e_interface_id_expected)
  732. else
  733. { in case of a generic or specialized interface we need to use the
  734. name of the def instead of the symbol, so that always the correct
  735. name is used }
  736. if [df_generic,df_specialization]*ttypesym(srsym).typedef.defoptions<>[] then
  737. sp:=tobjectdef(ttypesym(srsym).typedef).objname^;
  738. { must be a directly implemented interface }
  739. if Assigned(ImplIntf.ImplementsGetter) then
  740. Message2(parser_e_implements_no_mapping,ImplIntf.IntfDef.typename,astruct.objrealname^);
  741. consume(_ID);
  742. { Create unique name <interface>.<method> }
  743. hs:=sp+'.'+pattern;
  744. consume(_EQ);
  745. if assigned(ImplIntf) and
  746. (token=_ID) then
  747. ImplIntf.AddMapping(hs,pattern);
  748. consume(_ID);
  749. result:=true;
  750. exit;
  751. end;
  752. { method ? }
  753. srsym:=nil;
  754. if (consume_generic_type_parameter or not assigned(astruct)) and
  755. (symtablestack.top.symtablelevel=main_program_level) and
  756. try_to_consume(_POINT) then
  757. begin
  758. repeat
  759. searchagain:=false;
  760. if not assigned(astruct) and not assigned(srsym) then
  761. srsym:=search_object_name(sp,true);
  762. { consume proc name }
  763. procstartfilepos:=current_tokenpos;
  764. consume_proc_name;
  765. { qualifier is class name ? }
  766. if (srsym.typ=typesym) and
  767. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  768. begin
  769. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  770. if (token<>_POINT) then
  771. if (potype in [potype_class_constructor,potype_class_destructor]) then
  772. sp:=lower(sp)
  773. else
  774. if (potype=potype_operator)and(optoken=NOTOKEN) then
  775. parse_operator_name;
  776. srsym:=tsym(astruct.symtable.Find(sp));
  777. if assigned(srsym) then
  778. begin
  779. if srsym.typ=procsym then
  780. aprocsym:=tprocsym(srsym)
  781. else
  782. if (srsym.typ=typesym) and
  783. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  784. begin
  785. searchagain:=true;
  786. consume(_POINT);
  787. end
  788. else
  789. begin
  790. { we use a different error message for tp7 so it looks more compatible }
  791. if (m_fpc in current_settings.modeswitches) then
  792. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  793. else
  794. Message(parser_e_methode_id_expected);
  795. { rename the name to an unique name to avoid an
  796. error when inserting the symbol in the symtable }
  797. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  798. end;
  799. end
  800. else
  801. begin
  802. Message(parser_e_methode_id_expected);
  803. { recover by making it a normal procedure instead of method }
  804. astruct:=nil;
  805. end;
  806. end
  807. else
  808. Message(parser_e_class_id_expected);
  809. until not searchagain;
  810. end
  811. else
  812. begin
  813. { check for constructor/destructor/class operators which are not allowed here }
  814. if (not parse_only) and
  815. ((potype in [potype_constructor,potype_destructor,
  816. potype_class_constructor,potype_class_destructor]) or
  817. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  818. Message(parser_e_only_methods_allowed);
  819. repeat
  820. searchagain:=false;
  821. current_tokenpos:=procstartfilepos;
  822. if (potype=potype_operator)and(optoken=NOTOKEN) then
  823. parse_operator_name;
  824. srsym:=tsym(symtablestack.top.Find(sp));
  825. { Also look in the globalsymtable if we didn't found
  826. the symbol in the localsymtable }
  827. if not assigned(srsym) and
  828. not(parse_only) and
  829. (symtablestack.top=current_module.localsymtable) and
  830. assigned(current_module.globalsymtable) then
  831. srsym:=tsym(current_module.globalsymtable.Find(sp));
  832. { Check if overloaded is a procsym }
  833. if assigned(srsym) then
  834. begin
  835. if srsym.typ=procsym then
  836. aprocsym:=tprocsym(srsym)
  837. else
  838. begin
  839. { when the other symbol is a unit symbol then hide the unit
  840. symbol, this is not supported in tp7 }
  841. if not(m_tp7 in current_settings.modeswitches) and
  842. (srsym.typ=unitsym) then
  843. begin
  844. HideSym(srsym);
  845. searchagain:=true;
  846. end
  847. else
  848. if (m_delphi in current_settings.modeswitches) and
  849. (srsym.typ=absolutevarsym) and
  850. ([vo_is_funcret,vo_is_result]*tabstractvarsym(srsym).varoptions=[vo_is_funcret]) then
  851. begin
  852. HideSym(srsym);
  853. searchagain:=true;
  854. end
  855. else
  856. begin
  857. { we use a different error message for tp7 so it looks more compatible }
  858. if (m_fpc in current_settings.modeswitches) then
  859. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  860. else
  861. Message1(sym_e_duplicate_id,srsym.realname);
  862. { rename the name to an unique name to avoid an
  863. error when inserting the symbol in the symtable }
  864. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  865. end;
  866. end;
  867. end;
  868. until not searchagain;
  869. end;
  870. { test again if assigned, it can be reset to recover }
  871. if not assigned(aprocsym) then
  872. begin
  873. { create a new procsym and set the real filepos }
  874. current_tokenpos:=procstartfilepos;
  875. { for operator we have only one procsym for each overloaded
  876. operation }
  877. if (potype=potype_operator) then
  878. begin
  879. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  880. if aprocsym=nil then
  881. aprocsym:=cprocsym.create('$'+sp);
  882. end
  883. else
  884. if (potype in [potype_class_constructor,potype_class_destructor]) then
  885. aprocsym:=cprocsym.create('$'+lower(sp))
  886. else
  887. aprocsym:=cprocsym.create(orgsp);
  888. symtablestack.top.insert(aprocsym);
  889. end;
  890. { to get the correct symtablelevel we must ignore ObjectSymtables }
  891. st:=nil;
  892. checkstack:=symtablestack.stack;
  893. while assigned(checkstack) do
  894. begin
  895. st:=checkstack^.symtable;
  896. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  897. break;
  898. checkstack:=checkstack^.next;
  899. end;
  900. pd:=cprocdef.create(st.symtablelevel+1);
  901. pd.struct:=astruct;
  902. pd.procsym:=aprocsym;
  903. pd.proctypeoption:=potype;
  904. { methods inherit df_generic or df_specialization from the objectdef }
  905. if assigned(pd.struct) and
  906. (pd.parast.symtablelevel=normal_function_level) then
  907. begin
  908. if (df_generic in pd.struct.defoptions) then
  909. begin
  910. include(pd.defoptions,df_generic);
  911. parse_generic:=true;
  912. end;
  913. if (df_specialization in pd.struct.defoptions) then
  914. begin
  915. include(pd.defoptions,df_specialization);
  916. { Find corresponding genericdef, we need it later to
  917. replay the tokens to generate the body }
  918. if not assigned(pd.struct.genericdef) then
  919. internalerror(200512113);
  920. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  921. if not assigned(genericst) then
  922. internalerror(200512114);
  923. { when searching for the correct procdef to use as genericdef we need to ignore
  924. everything except procdefs so that we can find the correct indices }
  925. index:=0;
  926. found:=false;
  927. for i:=0 to pd.owner.deflist.count-1 do
  928. begin
  929. if tdef(pd.owner.deflist[i]).typ<>procdef then
  930. continue;
  931. if pd.owner.deflist[i]=pd then
  932. begin
  933. found:=true;
  934. break;
  935. end;
  936. inc(index);
  937. end;
  938. if not found then
  939. internalerror(2014052301);
  940. for i:=0 to genericst.deflist.count-1 do
  941. begin
  942. if tdef(genericst.deflist[i]).typ<>procdef then
  943. continue;
  944. if index=0 then
  945. pd.genericdef:=tstoreddef(genericst.deflist[i]);
  946. dec(index);
  947. end;
  948. if not assigned(pd.genericdef) or
  949. (pd.genericdef.typ<>procdef) then
  950. internalerror(200512115);
  951. end;
  952. end;
  953. { methods need to be exported }
  954. if assigned(astruct) and
  955. (
  956. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  957. (symtablestack.top.symtablelevel=main_program_level)
  958. ) then
  959. include(pd.procoptions,po_global);
  960. { symbol options that need to be kept per procdef }
  961. pd.fileinfo:=procstartfilepos;
  962. pd.visibility:=symtablestack.top.currentvisibility;
  963. if symtablestack.top.currentlyoptional then
  964. include(pd.procoptions,po_optional);
  965. { parse parameters }
  966. if token=_LKLAMMER then
  967. begin
  968. old_current_structdef:=nil;
  969. old_current_genericdef:=nil;
  970. old_current_specializedef:=nil;
  971. { Add ObjectSymtable to be able to find nested type definitions }
  972. popclass:=0;
  973. if assigned(pd.struct) and
  974. (pd.parast.symtablelevel>=normal_function_level) and
  975. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  976. begin
  977. popclass:=push_nested_hierarchy(pd.struct);
  978. old_current_structdef:=current_structdef;
  979. old_current_genericdef:=current_genericdef;
  980. old_current_specializedef:=current_specializedef;
  981. current_structdef:=pd.struct;
  982. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  983. current_genericdef:=current_structdef;
  984. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  985. current_specializedef:=current_structdef;
  986. end;
  987. { Add parameter symtable }
  988. if pd.parast.symtabletype<>staticsymtable then
  989. symtablestack.push(pd.parast);
  990. parse_parameter_dec(pd);
  991. if pd.parast.symtabletype<>staticsymtable then
  992. symtablestack.pop(pd.parast);
  993. if popclass>0 then
  994. begin
  995. current_structdef:=old_current_structdef;
  996. current_genericdef:=old_current_genericdef;
  997. current_specializedef:=old_current_specializedef;
  998. dec(popclass,pop_nested_hierarchy(pd.struct));
  999. if popclass<>0 then
  1000. internalerror(201011260); // 11 nov 2010 index 0
  1001. end;
  1002. end;
  1003. parse_generic:=old_parse_generic;
  1004. result:=true;
  1005. end;
  1006. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  1007. var
  1008. pd: tprocdef;
  1009. locationstr: string;
  1010. i: integer;
  1011. found: boolean;
  1012. old_block_type: tblock_type;
  1013. procedure read_returndef(pd: tprocdef);
  1014. var
  1015. popclass: integer;
  1016. old_parse_generic: boolean;
  1017. old_current_structdef: tabstractrecorddef;
  1018. old_current_genericdef,
  1019. old_current_specializedef: tstoreddef;
  1020. begin
  1021. old_parse_generic:=parse_generic;
  1022. { Add ObjectSymtable to be able to find generic type definitions }
  1023. popclass:=0;
  1024. old_current_structdef:=nil;
  1025. old_current_genericdef:=nil;
  1026. old_current_specializedef:=nil;
  1027. if assigned(pd.struct) and
  1028. (pd.parast.symtablelevel>=normal_function_level) and
  1029. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1030. begin
  1031. popclass:=push_nested_hierarchy(pd.struct);
  1032. parse_generic:=(df_generic in pd.struct.defoptions);
  1033. old_current_structdef:=current_structdef;
  1034. old_current_genericdef:=current_genericdef;
  1035. old_current_specializedef:=current_specializedef;
  1036. current_structdef:=pd.struct;
  1037. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1038. current_genericdef:=current_structdef;
  1039. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1040. current_specializedef:=current_structdef;
  1041. end;
  1042. single_type(pd.returndef,[stoAllowSpecialization]);
  1043. // Issue #24863, commented out for now because it breaks building of RTL and needs extensive
  1044. // testing and/or RTL patching.
  1045. {
  1046. if ((pd.returndef=cvarianttype) or (pd.returndef=colevarianttype)) and
  1047. not(cs_compilesystem in current_settings.moduleswitches) then
  1048. current_module.flags:=current_module.flags or uf_uses_variants;
  1049. }
  1050. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  1051. Message1(type_e_not_automatable,pd.returndef.typename);
  1052. if popclass>0 then
  1053. begin
  1054. current_structdef:=old_current_structdef;
  1055. current_genericdef:=old_current_genericdef;
  1056. current_specializedef:=old_current_specializedef;
  1057. dec(popclass,pop_nested_hierarchy(pd.struct));
  1058. if popclass<>0 then
  1059. internalerror(201012020);
  1060. end;
  1061. parse_generic:=old_parse_generic;
  1062. end;
  1063. begin
  1064. locationstr:='';
  1065. pd:=nil;
  1066. case token of
  1067. _FUNCTION :
  1068. begin
  1069. consume(_FUNCTION);
  1070. if parse_proc_head(astruct,potype_function,pd) then
  1071. begin
  1072. { pd=nil when it is a interface mapping }
  1073. if assigned(pd) then
  1074. begin
  1075. if try_to_consume(_COLON) then
  1076. begin
  1077. read_returndef(pd);
  1078. if (target_info.system in [system_m68k_amiga]) then
  1079. begin
  1080. if (idtoken=_LOCATION) then
  1081. begin
  1082. if po_explicitparaloc in pd.procoptions then
  1083. begin
  1084. consume(_LOCATION);
  1085. locationstr:=cstringpattern;
  1086. consume(_CSTRING);
  1087. end
  1088. else
  1089. { I guess this needs a new message... (KB) }
  1090. Message(parser_e_paraloc_all_paras);
  1091. end
  1092. else
  1093. begin
  1094. if po_explicitparaloc in pd.procoptions then
  1095. { assign default locationstr, if none specified }
  1096. { and we've arguments with explicit paraloc }
  1097. locationstr:='D0';
  1098. end;
  1099. end;
  1100. end
  1101. else
  1102. begin
  1103. if (
  1104. parse_only and
  1105. not(is_interface(pd.struct))
  1106. ) or
  1107. (m_repeat_forward in current_settings.modeswitches) then
  1108. begin
  1109. consume(_COLON);
  1110. consume_all_until(_SEMICOLON);
  1111. end;
  1112. end;
  1113. if isclassmethod then
  1114. include(pd.procoptions,po_classmethod);
  1115. end;
  1116. end
  1117. else
  1118. begin
  1119. { recover }
  1120. consume(_COLON);
  1121. consume_all_until(_SEMICOLON);
  1122. end;
  1123. end;
  1124. _PROCEDURE :
  1125. begin
  1126. consume(_PROCEDURE);
  1127. if parse_proc_head(astruct,potype_procedure,pd) then
  1128. begin
  1129. { pd=nil when it is an interface mapping }
  1130. if assigned(pd) then
  1131. begin
  1132. pd.returndef:=voidtype;
  1133. if isclassmethod then
  1134. include(pd.procoptions,po_classmethod);
  1135. end;
  1136. end;
  1137. end;
  1138. _CONSTRUCTOR :
  1139. begin
  1140. consume(_CONSTRUCTOR);
  1141. if isclassmethod then
  1142. parse_proc_head(astruct,potype_class_constructor,pd)
  1143. else
  1144. parse_proc_head(astruct,potype_constructor,pd);
  1145. if not isclassmethod and
  1146. assigned(pd) and
  1147. assigned(pd.struct) then
  1148. begin
  1149. { Set return type, class constructors return the
  1150. created instance, object constructors return boolean }
  1151. if is_class(pd.struct) or
  1152. is_record(pd.struct) or
  1153. is_javaclass(pd.struct) then
  1154. pd.returndef:=pd.struct
  1155. else
  1156. if is_objectpascal_helper(pd.struct) then
  1157. pd.returndef:=tobjectdef(pd.struct).extendeddef
  1158. else
  1159. {$ifdef CPU64bitaddr}
  1160. pd.returndef:=bool64type;
  1161. {$else CPU64bitaddr}
  1162. pd.returndef:=bool32type;
  1163. {$endif CPU64bitaddr}
  1164. end
  1165. else
  1166. pd.returndef:=voidtype;
  1167. end;
  1168. _DESTRUCTOR :
  1169. begin
  1170. consume(_DESTRUCTOR);
  1171. if isclassmethod then
  1172. parse_proc_head(astruct,potype_class_destructor,pd)
  1173. else
  1174. parse_proc_head(astruct,potype_destructor,pd);
  1175. if assigned(pd) then
  1176. pd.returndef:=voidtype;
  1177. end;
  1178. else
  1179. if (token=_OPERATOR) or
  1180. (isclassmethod and (idtoken=_OPERATOR)) then
  1181. begin
  1182. { we need to set the block type to bt_body, so that operator names
  1183. like ">", "=>" or "<>" are parsed correctly instead of e.g.
  1184. _LSHARPBRACKET and _RSHARPBRACKET for "<>" }
  1185. old_block_type:=block_type;
  1186. block_type:=bt_body;
  1187. consume(_OPERATOR);
  1188. parse_proc_head(astruct,potype_operator,pd);
  1189. block_type:=old_block_type;
  1190. if assigned(pd) then
  1191. begin
  1192. { operators always need to be searched in all units (that
  1193. contain operators) }
  1194. include(pd.procoptions,po_overload);
  1195. pd.procsym.owner.includeoption(sto_has_operator);
  1196. if pd.parast.symtablelevel>normal_function_level then
  1197. Message(parser_e_no_local_operator);
  1198. if isclassmethod then
  1199. include(pd.procoptions,po_classmethod);
  1200. if token<>_ID then
  1201. begin
  1202. if not(m_result in current_settings.modeswitches) then
  1203. consume(_ID);
  1204. end
  1205. else
  1206. begin
  1207. pd.resultname:=stringdup(orgpattern);
  1208. consume(_ID);
  1209. end;
  1210. if not try_to_consume(_COLON) then
  1211. begin
  1212. consume(_COLON);
  1213. pd.returndef:=generrordef;
  1214. consume_all_until(_SEMICOLON);
  1215. end
  1216. else
  1217. begin
  1218. read_returndef(pd);
  1219. { check that class operators have either return type of structure or }
  1220. { at least one argument of that type }
  1221. if (po_classmethod in pd.procoptions) and
  1222. (pd.returndef <> pd.struct) then
  1223. begin
  1224. found:=false;
  1225. for i := 0 to pd.parast.SymList.Count - 1 do
  1226. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1227. begin
  1228. found:=true;
  1229. break;
  1230. end;
  1231. if not found then
  1232. if assigned(pd.struct) then
  1233. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName)
  1234. else
  1235. MessagePos(pd.fileinfo,type_e_type_id_expected);
  1236. end;
  1237. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1238. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1239. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1240. message(parser_e_no_such_assignment)
  1241. else if not isoperatoracceptable(pd,optoken) then
  1242. Message(parser_e_overload_impossible);
  1243. end;
  1244. end
  1245. else
  1246. begin
  1247. { recover }
  1248. try_to_consume(_ID);
  1249. consume(_COLON);
  1250. consume_all_until(_SEMICOLON);
  1251. end;
  1252. end;
  1253. end;
  1254. { file types can't be function results }
  1255. if assigned(pd) and
  1256. (pd.returndef.typ=filedef) then
  1257. message(parser_e_illegal_function_result);
  1258. { support procedure proc stdcall export; }
  1259. if not(check_proc_directive(false)) then
  1260. begin
  1261. if (token=_COLON) then
  1262. begin
  1263. message(parser_e_field_not_allowed_here);
  1264. consume_all_until(_SEMICOLON);
  1265. end;
  1266. consume(_SEMICOLON);
  1267. end;
  1268. result:=pd;
  1269. if locationstr<>'' then
  1270. begin
  1271. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1272. { I guess this needs a new message... (KB) }
  1273. message(parser_e_illegal_explicit_paraloc);
  1274. end;
  1275. end;
  1276. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  1277. var
  1278. oldparse_only: boolean;
  1279. begin
  1280. oldparse_only:=parse_only;
  1281. parse_only:=true;
  1282. result:=parse_proc_dec(is_classdef,astruct);
  1283. { this is for error recovery as well as forward }
  1284. { interface mappings, i.e. mapping to a method }
  1285. { which isn't declared yet }
  1286. if assigned(result) then
  1287. begin
  1288. parse_record_proc_directives(result);
  1289. { since records have no inheritance, don't allow non-static
  1290. class methods. Selphi does the same. }
  1291. if (result.proctypeoption<>potype_operator) and
  1292. is_classdef and
  1293. not (po_staticmethod in result.procoptions) then
  1294. MessagePos(result.fileinfo, parser_e_class_methods_only_static_in_records);
  1295. // we can't add hidden params here because record is not yet defined
  1296. // and therefore record size which has influence on paramter passing rules may change too
  1297. // look at record_dec to see where calling conventions are applied (issue #0021044)
  1298. handle_calling_convention(result,[hcc_check]);
  1299. { add definition to procsym }
  1300. proc_add_definition(result);
  1301. end;
  1302. maybe_parse_hint_directives(result);
  1303. parse_only:=oldparse_only;
  1304. end;
  1305. procedure insert_record_hidden_paras(astruct: trecorddef);
  1306. var
  1307. pd: tdef;
  1308. i: longint;
  1309. oldpos : tfileposinfo;
  1310. oldparse_only: boolean;
  1311. begin
  1312. // handle calling conventions of record methods
  1313. oldpos:=current_filepos;
  1314. oldparse_only:=parse_only;
  1315. parse_only:=true;
  1316. { don't keep track of procdefs in a separate list, because the
  1317. compiler may add additional procdefs (e.g. property wrappers for
  1318. the jvm backend) }
  1319. for i := 0 to astruct.symtable.deflist.count - 1 do
  1320. begin
  1321. pd:=tdef(astruct.symtable.deflist[i]);
  1322. if pd.typ<>procdef then
  1323. continue;
  1324. current_filepos:=tprocdef(pd).fileinfo;
  1325. handle_calling_convention(tprocdef(pd),[hcc_insert_hidden_paras]);
  1326. end;
  1327. parse_only:=oldparse_only;
  1328. current_filepos:=oldpos;
  1329. end;
  1330. {****************************************************************************
  1331. Procedure directive handlers
  1332. ****************************************************************************}
  1333. procedure pd_far(pd:tabstractprocdef);
  1334. begin
  1335. pd.declared_far;
  1336. end;
  1337. procedure pd_near(pd:tabstractprocdef);
  1338. begin
  1339. pd.declared_near;
  1340. end;
  1341. procedure pd_export(pd:tabstractprocdef);
  1342. begin
  1343. if pd.typ<>procdef then
  1344. internalerror(200304264);
  1345. if assigned(tprocdef(pd).struct) then
  1346. Message(parser_e_methods_dont_be_export);
  1347. if pd.parast.symtablelevel>normal_function_level then
  1348. Message(parser_e_dont_nest_export);
  1349. end;
  1350. procedure pd_forward(pd:tabstractprocdef);
  1351. begin
  1352. if pd.typ<>procdef then
  1353. internalerror(200304265);
  1354. tprocdef(pd).forwarddef:=true;
  1355. end;
  1356. procedure pd_alias(pd:tabstractprocdef);
  1357. begin
  1358. if pd.typ<>procdef then
  1359. internalerror(200304266);
  1360. consume(_COLON);
  1361. tprocdef(pd).aliasnames.insert(get_stringconst);
  1362. include(pd.procoptions,po_has_public_name);
  1363. end;
  1364. procedure pd_public(pd:tabstractprocdef);
  1365. begin
  1366. if pd.typ<>procdef then
  1367. internalerror(200304266);
  1368. if try_to_consume(_NAME) then
  1369. begin
  1370. tprocdef(pd).aliasnames.insert(get_stringconst);
  1371. include(pd.procoptions,po_has_public_name);
  1372. end;
  1373. end;
  1374. procedure pd_asmname(pd:tabstractprocdef);
  1375. begin
  1376. if pd.typ<>procdef then
  1377. internalerror(200304267);
  1378. if token=_CCHAR then
  1379. begin
  1380. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1381. consume(_CCHAR)
  1382. end
  1383. else
  1384. begin
  1385. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1386. consume(_CSTRING);
  1387. end;
  1388. { we don't need anything else }
  1389. tprocdef(pd).forwarddef:=false;
  1390. end;
  1391. procedure pd_internconst(pd:tabstractprocdef);
  1392. var v:Tconstexprint;
  1393. begin
  1394. if pd.typ<>procdef then
  1395. internalerror(200304268);
  1396. consume(_COLON);
  1397. v:=get_intconst;
  1398. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1399. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1400. else
  1401. Tprocdef(pd).extnumber:=longint(v.svalue);
  1402. end;
  1403. procedure pd_internproc(pd:tabstractprocdef);
  1404. var v:Tconstexprint;
  1405. begin
  1406. if pd.typ<>procdef then
  1407. internalerror(200304268);
  1408. consume(_COLON);
  1409. v:=get_intconst;
  1410. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1411. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1412. else
  1413. Tprocdef(pd).extnumber:=longint(v.svalue);
  1414. { the proc is defined }
  1415. tprocdef(pd).forwarddef:=false;
  1416. end;
  1417. procedure pd_interrupt(pd:tabstractprocdef);
  1418. begin
  1419. if pd.parast.symtablelevel>normal_function_level then
  1420. Message(parser_e_dont_nest_interrupt);
  1421. end;
  1422. procedure pd_abstract(pd:tabstractprocdef);
  1423. begin
  1424. if pd.typ<>procdef then
  1425. internalerror(200304269);
  1426. if is_objectpascal_helper(tprocdef(pd).struct) then
  1427. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1428. if assigned(tprocdef(pd).struct) and
  1429. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1430. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1431. else if (po_virtualmethod in pd.procoptions) then
  1432. begin
  1433. include(pd.procoptions,po_abstractmethod);
  1434. { one more abstract method }
  1435. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1436. end
  1437. else
  1438. Message(parser_e_only_virtual_methods_abstract);
  1439. { the method is defined }
  1440. tprocdef(pd).forwarddef:=false;
  1441. end;
  1442. procedure pd_final(pd:tabstractprocdef);
  1443. begin
  1444. if pd.typ<>procdef then
  1445. internalerror(200910170);
  1446. if is_objectpascal_helper(tprocdef(pd).struct) and
  1447. (m_objfpc in current_settings.modeswitches) then
  1448. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1449. if (po_virtualmethod in pd.procoptions) or
  1450. (is_javaclass(tprocdef(pd).struct) and
  1451. (po_classmethod in pd.procoptions)) then
  1452. include(pd.procoptions,po_finalmethod)
  1453. else
  1454. Message(parser_e_only_virtual_methods_final);
  1455. end;
  1456. procedure pd_enumerator(pd:tabstractprocdef);
  1457. begin
  1458. if pd.typ<>procdef then
  1459. internalerror(200910250);
  1460. if (token = _ID) then
  1461. begin
  1462. if pattern='MOVENEXT' then
  1463. begin
  1464. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1465. message(parser_e_only_one_enumerator_movenext);
  1466. pd.calcparas;
  1467. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1468. (pd.minparacount = 0) then
  1469. begin
  1470. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1471. include(pd.procoptions,po_enumerator_movenext);
  1472. end
  1473. else
  1474. Message(parser_e_enumerator_movenext_is_not_valid)
  1475. end
  1476. else
  1477. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1478. consume(token);
  1479. end
  1480. else
  1481. Message(parser_e_enumerator_identifier_required);
  1482. end;
  1483. procedure pd_virtual(pd:tabstractprocdef);
  1484. {$ifdef WITHDMT}
  1485. var
  1486. pt : tnode;
  1487. {$endif WITHDMT}
  1488. begin
  1489. if (not assigned(pd.owner.defowner) or
  1490. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1491. (po_external in pd.procoptions) then
  1492. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1493. if pd.typ<>procdef then
  1494. internalerror(2003042610);
  1495. if (pd.proctypeoption=potype_constructor) and
  1496. is_object(tprocdef(pd).struct) then
  1497. Message(parser_e_constructor_cannot_be_not_virtual);
  1498. if is_objectpascal_helper(tprocdef(pd).struct) and
  1499. (m_objfpc in current_settings.modeswitches) then
  1500. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1501. {$ifdef WITHDMT}
  1502. if is_object(tprocdef(pd).struct) and
  1503. (token<>_SEMICOLON) then
  1504. begin
  1505. { any type of parameter is allowed here! }
  1506. pt:=comp_expr(true);
  1507. if is_constintnode(pt) then
  1508. begin
  1509. include(pd.procoptions,po_msgint);
  1510. pd.messageinf.i:=pt.value;
  1511. end
  1512. else
  1513. Message(parser_e_ill_msg_expr);
  1514. disposetree(pt);
  1515. end;
  1516. {$endif WITHDMT}
  1517. end;
  1518. procedure pd_dispid(pd:tabstractprocdef);
  1519. var pt:Tnode;
  1520. begin
  1521. if pd.typ<>procdef then
  1522. internalerror(200604301);
  1523. pt:=comp_expr(true,false);
  1524. if is_constintnode(pt) then
  1525. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1526. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1527. else
  1528. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1529. else
  1530. message(parser_e_dispid_must_be_ord_const);
  1531. pt.free;
  1532. end;
  1533. procedure pd_static(pd:tabstractprocdef);
  1534. begin
  1535. if pd.typ<>procdef then
  1536. internalerror(2013032001);
  1537. if not assigned(tprocdef(pd).struct) then
  1538. internalerror(2013032002);
  1539. include(tprocdef(pd).procsym.symoptions,sp_static);
  1540. { "static" is not allowed for operators or normal methods (except in objects) }
  1541. if (pd.proctypeoption=potype_operator) or
  1542. (
  1543. not (po_classmethod in pd.procoptions) and
  1544. not is_object(tprocdef(pd).struct)
  1545. )
  1546. then
  1547. Message1(parser_e_dir_not_allowed,arraytokeninfo[_STATIC].str);
  1548. include(pd.procoptions,po_staticmethod);
  1549. end;
  1550. procedure pd_override(pd:tabstractprocdef);
  1551. begin
  1552. if pd.typ<>procdef then
  1553. internalerror(2003042611);
  1554. if is_objectpascal_helper(tprocdef(pd).struct) then
  1555. begin
  1556. if m_objfpc in current_settings.modeswitches then
  1557. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1558. end
  1559. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1560. Message(parser_e_no_object_override)
  1561. else if is_objccategory(tprocdef(pd).struct) then
  1562. Message(parser_e_no_category_override)
  1563. else if (po_external in pd.procoptions) and
  1564. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1565. not is_cppclass(tprocdef(pd).struct) and
  1566. not is_java_class_or_interface(tprocdef(pd).struct) then
  1567. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1568. end;
  1569. procedure pd_overload(pd:tabstractprocdef);
  1570. begin
  1571. if pd.typ<>procdef then
  1572. internalerror(2003042612);
  1573. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1574. end;
  1575. procedure pd_message(pd:tabstractprocdef);
  1576. var
  1577. pt : tnode;
  1578. paracnt : longint;
  1579. begin
  1580. if pd.typ<>procdef then
  1581. internalerror(2003042613);
  1582. if is_objectpascal_helper(tprocdef(pd).struct) then
  1583. begin
  1584. if m_objfpc in current_settings.modeswitches then
  1585. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1586. end
  1587. else
  1588. if not is_class(tprocdef(pd).struct) and
  1589. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1590. Message(parser_e_msg_only_for_classes);
  1591. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1592. Message(parser_e_multiple_messages);
  1593. { check parameter type }
  1594. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1595. begin
  1596. if po_external in pd.procoptions then
  1597. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1598. paracnt:=0;
  1599. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1600. if paracnt<>1 then
  1601. Message(parser_e_ill_msg_param);
  1602. end;
  1603. pt:=comp_expr(true,false);
  1604. { message is 1-character long }
  1605. if is_constcharnode(pt) then
  1606. begin
  1607. include(pd.procoptions,po_msgstr);
  1608. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1609. end
  1610. else if pt.nodetype=stringconstn then
  1611. begin
  1612. include(pd.procoptions,po_msgstr);
  1613. if (tstringconstnode(pt).len>255) then
  1614. Message(parser_e_message_string_too_long);
  1615. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1616. end
  1617. else
  1618. if is_constintnode(pt) and
  1619. (is_class(tprocdef(pd).struct) or
  1620. is_objectpascal_helper(tprocdef(pd).struct)) then
  1621. begin
  1622. include(pd.procoptions,po_msgint);
  1623. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1624. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1625. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  1626. else
  1627. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1628. end
  1629. else
  1630. Message(parser_e_ill_msg_expr);
  1631. { check whether the selector name is valid in case of Objective-C }
  1632. if (po_msgstr in pd.procoptions) and
  1633. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1634. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1635. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1636. pt.free;
  1637. end;
  1638. procedure pd_reintroduce(pd:tabstractprocdef);
  1639. begin
  1640. if pd.typ<>procdef then
  1641. internalerror(200401211);
  1642. if is_objectpascal_helper(tprocdef(pd).struct) then
  1643. begin
  1644. if m_objfpc in current_settings.modeswitches then
  1645. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1646. end
  1647. else
  1648. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1649. not(is_objccategory(tprocdef(pd).struct)) and
  1650. not(is_javaclass(tprocdef(pd).struct)) then
  1651. Message(parser_e_no_object_reintroduce);
  1652. end;
  1653. procedure pd_syscall(pd:tabstractprocdef);
  1654. {$if defined(powerpc) or defined(m68k)}
  1655. var
  1656. vs : tparavarsym;
  1657. sym : tsym;
  1658. symtable : TSymtable;
  1659. v: Tconstexprint;
  1660. {$endif defined(powerpc) or defined(m68k)}
  1661. begin
  1662. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1663. internalerror(2003042614);
  1664. tprocdef(pd).forwarddef:=false;
  1665. {$ifdef m68k}
  1666. if target_info.system in [system_m68k_amiga] then
  1667. begin
  1668. include(pd.procoptions,po_syscall_legacy);
  1669. if consume_sym(sym,symtable) then
  1670. begin
  1671. if (sym.typ=staticvarsym) and
  1672. (
  1673. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1674. is_32bitint(tabstractvarsym(sym).vardef)
  1675. ) then
  1676. begin
  1677. tcpuprocdef(pd).libsym:=sym;
  1678. if po_syscall_legacy in tprocdef(pd).procoptions then
  1679. begin
  1680. vs:=cparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1681. paramanager.parseparaloc(vs,'A6');
  1682. pd.parast.insert(vs);
  1683. end
  1684. end
  1685. else
  1686. Message(parser_e_32bitint_or_pointer_variable_expected);
  1687. end;
  1688. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1689. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1690. v:=get_intconst;
  1691. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1692. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  1693. else
  1694. Tprocdef(pd).extnumber:=v.uvalue;
  1695. end;
  1696. {$endif m68k}
  1697. {$ifdef powerpc}
  1698. if target_info.system = system_powerpc_amiga then
  1699. begin
  1700. include(pd.procoptions,po_syscall_basesysv);
  1701. if consume_sym(sym,symtable) then
  1702. begin
  1703. if (sym.typ=staticvarsym) and
  1704. (
  1705. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1706. is_32bitint(tabstractvarsym(sym).vardef)
  1707. ) then
  1708. begin
  1709. tcpuprocdef(pd).libsym:=sym;
  1710. vs:=cparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1711. pd.parast.insert(vs);
  1712. end
  1713. else
  1714. Message(parser_e_32bitint_or_pointer_variable_expected);
  1715. end;
  1716. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1717. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1718. v:=get_intconst;
  1719. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1720. message(parser_e_range_check_error)
  1721. else
  1722. Tprocdef(pd).extnumber:=v.uvalue;
  1723. end else
  1724. if target_info.system = system_powerpc_morphos then
  1725. begin
  1726. if idtoken=_LEGACY then
  1727. begin
  1728. consume(_LEGACY);
  1729. include(pd.procoptions,po_syscall_legacy);
  1730. end
  1731. else if idtoken=_SYSV then
  1732. begin
  1733. consume(_SYSV);
  1734. include(pd.procoptions,po_syscall_sysv);
  1735. end
  1736. else if idtoken=_BASESYSV then
  1737. begin
  1738. consume(_BASESYSV);
  1739. include(pd.procoptions,po_syscall_basesysv);
  1740. end
  1741. else if idtoken=_SYSVBASE then
  1742. begin
  1743. consume(_SYSVBASE);
  1744. include(pd.procoptions,po_syscall_sysvbase);
  1745. end
  1746. else if idtoken=_R12BASE then
  1747. begin
  1748. consume(_R12BASE);
  1749. include(pd.procoptions,po_syscall_r12base);
  1750. end
  1751. else
  1752. if syscall_convention='LEGACY' then
  1753. include(pd.procoptions,po_syscall_legacy)
  1754. else if syscall_convention='SYSV' then
  1755. include(pd.procoptions,po_syscall_sysv)
  1756. else if syscall_convention='BASESYSV' then
  1757. include(pd.procoptions,po_syscall_basesysv)
  1758. else if syscall_convention='SYSVBASE' then
  1759. include(pd.procoptions,po_syscall_sysvbase)
  1760. else if syscall_convention='R12BASE' then
  1761. include(pd.procoptions,po_syscall_r12base)
  1762. else
  1763. internalerror(2005010404);
  1764. if consume_sym(sym,symtable) then
  1765. begin
  1766. if (sym.typ=staticvarsym) and
  1767. (
  1768. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1769. is_32bitint(tabstractvarsym(sym).vardef)
  1770. ) then
  1771. begin
  1772. tcpuprocdef(pd).libsym:=sym;
  1773. if po_syscall_legacy in tprocdef(pd).procoptions then
  1774. begin
  1775. vs:=cparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1776. paramanager.parseparaloc(vs,'A6');
  1777. pd.parast.insert(vs);
  1778. end
  1779. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1780. begin
  1781. { Nothing to be done for sysv here for now, but this might change }
  1782. end
  1783. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1784. begin
  1785. vs:=cparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1786. pd.parast.insert(vs);
  1787. end
  1788. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1789. begin
  1790. vs:=cparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1791. pd.parast.insert(vs);
  1792. end
  1793. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1794. begin
  1795. vs:=cparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1796. paramanager.parseparaloc(vs,'R12');
  1797. pd.parast.insert(vs);
  1798. end
  1799. else
  1800. internalerror(2005010501);
  1801. end
  1802. else
  1803. Message(parser_e_32bitint_or_pointer_variable_expected);
  1804. end;
  1805. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1806. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1807. v:=get_intconst;
  1808. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1809. message(parser_e_range_check_error)
  1810. else
  1811. Tprocdef(pd).extnumber:=v.uvalue;
  1812. end;
  1813. {$endif powerpc}
  1814. end;
  1815. procedure pd_external(pd:tabstractprocdef);
  1816. {
  1817. If import_dll=nil the procedure is assumed to be in another
  1818. object file. In that object file it should have the name to
  1819. which import_name is pointing to. Otherwise, the procedure is
  1820. assumed to be in the DLL to which import_dll is pointing to. In
  1821. that case either import_nr<>0 or import_name<>nil is true, so
  1822. the procedure is either imported by number or by name. (DM)
  1823. }
  1824. var
  1825. hs : string;
  1826. v:Tconstexprint;
  1827. is_java_external: boolean;
  1828. begin
  1829. if pd.typ<>procdef then
  1830. internalerror(2003042615);
  1831. { Allow specifying a separate external name for methods in external Java
  1832. because its identifier naming constraints are laxer than FPC's
  1833. (e.g., case sensitive).
  1834. Limitation: only allows specifying the symbol name and not the package name,
  1835. and only for external classes/interfaces }
  1836. is_java_external:=
  1837. (pd.typ=procdef) and
  1838. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  1839. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  1840. with tprocdef(pd) do
  1841. begin
  1842. forwarddef:=false;
  1843. { forbid local external procedures }
  1844. if parast.symtablelevel>normal_function_level then
  1845. Message(parser_e_no_local_proc_external);
  1846. { If the procedure should be imported from a DLL, a constant string follows.
  1847. This isn't really correct, an contant string expression follows
  1848. so we check if an semicolon follows, else a string constant have to
  1849. follow (FK) }
  1850. if not is_java_external and
  1851. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1852. begin
  1853. { Always add library prefix and suffix to create an uniform name }
  1854. hs:=get_stringconst;
  1855. if ExtractFileExt(hs)='' then
  1856. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1857. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1858. hs:=target_info.sharedlibprefix+hs;
  1859. { the JVM expects java/lang/Object rather than java.lang.Object }
  1860. if target_info.system in systems_jvm then
  1861. Replace(hs,'.','/');
  1862. import_dll:=stringdup(hs);
  1863. include(procoptions,po_has_importdll);
  1864. if (idtoken=_NAME) then
  1865. begin
  1866. consume(_NAME);
  1867. import_name:=stringdup(get_stringconst);
  1868. include(procoptions,po_has_importname);
  1869. if import_name^='' then
  1870. message(parser_e_empty_import_name);
  1871. end;
  1872. if (idtoken=_INDEX) then
  1873. begin
  1874. {After the word index follows the index number in the DLL.}
  1875. consume(_INDEX);
  1876. v:=get_intconst;
  1877. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1878. message(parser_e_range_check_error)
  1879. else
  1880. import_nr:=longint(v.svalue);
  1881. end;
  1882. { default is to used the realname of the procedure }
  1883. if (import_nr=0) and not assigned(import_name) then
  1884. begin
  1885. import_name:=stringdup(procsym.realname);
  1886. include(procoptions,po_has_importname);
  1887. end;
  1888. end
  1889. else
  1890. begin
  1891. if (idtoken=_NAME) or
  1892. is_java_external then
  1893. begin
  1894. consume(_NAME);
  1895. import_name:=stringdup(get_stringconst);
  1896. include(procoptions,po_has_importname);
  1897. if import_name^='' then
  1898. message(parser_e_empty_import_name);
  1899. end;
  1900. end;
  1901. end;
  1902. end;
  1903. procedure pd_weakexternal(pd:tabstractprocdef);
  1904. begin
  1905. if not(target_info.system in systems_weak_linking) then
  1906. message(parser_e_weak_external_not_supported)
  1907. else
  1908. pd_external(pd);
  1909. end;
  1910. type
  1911. pd_handler=procedure(pd:tabstractprocdef);
  1912. proc_dir_rec=record
  1913. idtok : ttoken;
  1914. pd_flags : tpdflags;
  1915. handler : pd_handler;
  1916. pocall : tproccalloption;
  1917. pooption : tprocoptions;
  1918. mutexclpocall : tproccalloptions;
  1919. mutexclpotype : tproctypeoptions;
  1920. mutexclpo : tprocoptions;
  1921. end;
  1922. const
  1923. {Should contain the number of procedure directives we support.}
  1924. num_proc_directives=44;
  1925. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1926. (
  1927. (
  1928. idtok:_ABSTRACT;
  1929. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  1930. handler : @pd_abstract;
  1931. pocall : pocall_none;
  1932. pooption : [po_abstractmethod];
  1933. mutexclpocall : [pocall_internproc];
  1934. mutexclpotype : [];
  1935. mutexclpo : [po_exports,po_interrupt,po_inline]
  1936. ),(
  1937. idtok:_ALIAS;
  1938. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1939. handler : @pd_alias;
  1940. pocall : pocall_none;
  1941. pooption : [];
  1942. mutexclpocall : [];
  1943. mutexclpotype : [];
  1944. mutexclpo : [po_external,po_inline]
  1945. ),(
  1946. idtok:_ASMNAME;
  1947. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1948. handler : @pd_asmname;
  1949. pocall : pocall_cdecl;
  1950. pooption : [po_external];
  1951. mutexclpocall : [pocall_internproc];
  1952. mutexclpotype : [];
  1953. mutexclpo : [po_external,po_inline]
  1954. ),(
  1955. idtok:_ASSEMBLER;
  1956. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1957. handler : nil;
  1958. pocall : pocall_none;
  1959. pooption : [po_assembler];
  1960. mutexclpocall : [];
  1961. mutexclpotype : [];
  1962. mutexclpo : [po_external]
  1963. ),(
  1964. idtok:_C; {same as cdecl for mode mac}
  1965. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1966. handler : nil;
  1967. pocall : pocall_cdecl;
  1968. pooption : [];
  1969. mutexclpocall : [];
  1970. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1971. mutexclpo : [po_assembler,po_external]
  1972. ),(
  1973. idtok:_CDECL;
  1974. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1975. handler : nil;
  1976. pocall : pocall_cdecl;
  1977. pooption : [];
  1978. mutexclpocall : [];
  1979. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1980. mutexclpo : [po_assembler,po_external]
  1981. ),(
  1982. idtok:_DISPID;
  1983. pd_flags : [pd_dispinterface];
  1984. handler : @pd_dispid;
  1985. pocall : pocall_none;
  1986. pooption : [po_dispid];
  1987. mutexclpocall : [pocall_internproc];
  1988. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1989. mutexclpo : [po_interrupt,po_external,po_inline]
  1990. ),(
  1991. idtok:_DYNAMIC;
  1992. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  1993. handler : @pd_virtual;
  1994. pocall : pocall_none;
  1995. pooption : [po_virtualmethod];
  1996. mutexclpocall : [pocall_internproc];
  1997. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  1998. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1999. ),(
  2000. idtok:_EXPORT;
  2001. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  2002. handler : @pd_export;
  2003. pocall : pocall_none;
  2004. pooption : [po_exports,po_global];
  2005. mutexclpocall : [pocall_internproc];
  2006. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2007. mutexclpo : [po_external,po_interrupt,po_inline]
  2008. ),(
  2009. idtok:_EXTERNAL;
  2010. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  2011. handler : @pd_external;
  2012. pocall : pocall_none;
  2013. pooption : [po_external];
  2014. mutexclpocall : [pocall_syscall];
  2015. { allowed for external cpp classes }
  2016. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2017. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2018. ),(
  2019. idtok:_FAR;
  2020. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2021. handler : @pd_far;
  2022. pocall : pocall_none;
  2023. pooption : [];
  2024. mutexclpocall : [pocall_internproc];
  2025. mutexclpotype : [];
  2026. mutexclpo : [po_inline]
  2027. ),(
  2028. idtok:_FAR16;
  2029. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  2030. handler : nil;
  2031. pocall : pocall_far16;
  2032. pooption : [];
  2033. mutexclpocall : [];
  2034. mutexclpotype : [];
  2035. mutexclpo : [po_external]
  2036. ),(
  2037. idtok:_FINAL;
  2038. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2039. handler : @pd_final;
  2040. pocall : pocall_none;
  2041. pooption : [po_finalmethod];
  2042. mutexclpocall : [pocall_internproc];
  2043. mutexclpotype : [];
  2044. mutexclpo : [po_exports,po_interrupt,po_inline]
  2045. ),(
  2046. idtok:_FORWARD;
  2047. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2048. handler : @pd_forward;
  2049. pocall : pocall_none;
  2050. pooption : [];
  2051. mutexclpocall : [pocall_internproc];
  2052. mutexclpotype : [];
  2053. mutexclpo : [po_external,po_inline]
  2054. ),(
  2055. idtok:_OLDFPCCALL;
  2056. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2057. handler : nil;
  2058. pocall : pocall_oldfpccall;
  2059. pooption : [];
  2060. mutexclpocall : [];
  2061. mutexclpotype : [];
  2062. mutexclpo : []
  2063. ),(
  2064. idtok:_INLINE;
  2065. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2066. handler : nil;
  2067. pocall : pocall_none;
  2068. pooption : [po_inline];
  2069. mutexclpocall : [pocall_safecall];
  2070. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2071. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  2072. ),(
  2073. idtok:_INTERNCONST;
  2074. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2075. handler : @pd_internconst;
  2076. pocall : pocall_none;
  2077. pooption : [po_internconst];
  2078. mutexclpocall : [];
  2079. mutexclpotype : [potype_operator];
  2080. mutexclpo : []
  2081. ),(
  2082. idtok:_INTERNPROC;
  2083. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2084. handler : @pd_internproc;
  2085. pocall : pocall_internproc;
  2086. pooption : [];
  2087. mutexclpocall : [];
  2088. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2089. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2090. ),(
  2091. idtok:_INTERRUPT;
  2092. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2093. handler : @pd_interrupt;
  2094. pocall : pocall_oldfpccall;
  2095. pooption : [po_interrupt];
  2096. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,pocall_mwpascal,
  2097. pocall_pascal,pocall_far16,pocall_oldfpccall];
  2098. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2099. mutexclpo : [po_external,po_inline]
  2100. ),(
  2101. idtok:_IOCHECK;
  2102. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2103. handler : nil;
  2104. pocall : pocall_none;
  2105. pooption : [po_iocheck];
  2106. mutexclpocall : [pocall_internproc];
  2107. mutexclpotype : [];
  2108. mutexclpo : [po_external]
  2109. ),(
  2110. idtok:_LOCAL;
  2111. pd_flags : [pd_implemen,pd_body];
  2112. handler : nil;
  2113. pocall : pocall_none;
  2114. pooption : [po_kylixlocal];
  2115. mutexclpocall : [pocall_internproc,pocall_far16];
  2116. mutexclpotype : [];
  2117. mutexclpo : [po_external,po_exports]
  2118. ),(
  2119. idtok:_MESSAGE;
  2120. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2121. handler : @pd_message;
  2122. pocall : pocall_none;
  2123. pooption : []; { can be po_msgstr or po_msgint }
  2124. mutexclpocall : [pocall_internproc];
  2125. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2126. mutexclpo : [po_interrupt,po_inline]
  2127. ),(
  2128. idtok:_MWPASCAL;
  2129. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2130. handler : nil;
  2131. pocall : pocall_mwpascal;
  2132. pooption : [];
  2133. mutexclpocall : [];
  2134. mutexclpotype : [];
  2135. mutexclpo : []
  2136. ),(
  2137. idtok:_NEAR;
  2138. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2139. handler : @pd_near;
  2140. pocall : pocall_none;
  2141. pooption : [];
  2142. mutexclpocall : [pocall_internproc];
  2143. mutexclpotype : [];
  2144. mutexclpo : []
  2145. ),(
  2146. idtok:_NORETURN;
  2147. pd_flags : [pd_implemen,pd_interface,pd_body,pd_notobjintf];
  2148. handler : nil;
  2149. pocall : pocall_none;
  2150. pooption : [po_noreturn];
  2151. mutexclpocall : [];
  2152. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2153. mutexclpo : [po_interrupt,po_virtualmethod,po_iocheck]
  2154. ),(
  2155. idtok:_NOSTACKFRAME;
  2156. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2157. handler : nil;
  2158. pocall : pocall_none;
  2159. pooption : [po_nostackframe];
  2160. mutexclpocall : [pocall_internproc];
  2161. mutexclpotype : [];
  2162. mutexclpo : []
  2163. ),(
  2164. idtok:_OVERLOAD;
  2165. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava,pd_objcclass,pd_objcprot];
  2166. handler : @pd_overload;
  2167. pocall : pocall_none;
  2168. pooption : [po_overload];
  2169. mutexclpocall : [pocall_internproc];
  2170. mutexclpotype : [];
  2171. mutexclpo : []
  2172. ),(
  2173. idtok:_OVERRIDE;
  2174. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2175. handler : @pd_override;
  2176. pocall : pocall_none;
  2177. pooption : [po_overridingmethod,po_virtualmethod];
  2178. mutexclpocall : [pocall_internproc];
  2179. mutexclpotype : [];
  2180. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2181. ),(
  2182. idtok:_PASCAL;
  2183. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2184. handler : nil;
  2185. pocall : pocall_pascal;
  2186. pooption : [];
  2187. mutexclpocall : [];
  2188. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2189. mutexclpo : [po_external]
  2190. ),(
  2191. idtok:_PUBLIC;
  2192. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2193. handler : @pd_public;
  2194. pocall : pocall_none;
  2195. pooption : [po_public,po_global];
  2196. mutexclpocall : [pocall_internproc];
  2197. mutexclpotype : [];
  2198. mutexclpo : [po_external,po_inline]
  2199. ),(
  2200. idtok:_REGISTER;
  2201. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2202. handler : nil;
  2203. pocall : pocall_register;
  2204. pooption : [];
  2205. mutexclpocall : [];
  2206. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2207. mutexclpo : [po_external]
  2208. ),(
  2209. idtok:_REINTRODUCE;
  2210. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2211. handler : @pd_reintroduce;
  2212. pocall : pocall_none;
  2213. pooption : [po_reintroduce];
  2214. mutexclpocall : [pocall_internproc];
  2215. mutexclpotype : [];
  2216. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2217. ),(
  2218. idtok:_SAFECALL;
  2219. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2220. handler : nil;
  2221. pocall : pocall_safecall;
  2222. pooption : [];
  2223. mutexclpocall : [];
  2224. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2225. mutexclpo : [po_external]
  2226. ),(
  2227. idtok:_SOFTFLOAT;
  2228. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2229. handler : nil;
  2230. pocall : pocall_softfloat;
  2231. pooption : [];
  2232. mutexclpocall : [];
  2233. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2234. { it's available with po_external because the libgcc floating point routines on the arm
  2235. uses this calling convention }
  2236. mutexclpo : []
  2237. ),(
  2238. idtok:_STATIC;
  2239. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2240. handler : @pd_static;
  2241. pocall : pocall_none;
  2242. pooption : [po_staticmethod];
  2243. mutexclpocall : [pocall_internproc];
  2244. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2245. mutexclpo : [po_interrupt,po_exports]
  2246. ),(
  2247. idtok:_STDCALL;
  2248. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2249. handler : nil;
  2250. pocall : pocall_stdcall;
  2251. pooption : [];
  2252. mutexclpocall : [];
  2253. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2254. mutexclpo : [po_external]
  2255. ),(
  2256. idtok:_SYSCALL;
  2257. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2258. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2259. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2260. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2261. handler : @pd_syscall;
  2262. pocall : pocall_syscall;
  2263. pooption : [];
  2264. mutexclpocall : [];
  2265. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2266. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2267. ),(
  2268. idtok:_VIRTUAL;
  2269. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2270. handler : @pd_virtual;
  2271. pocall : pocall_none;
  2272. pooption : [po_virtualmethod];
  2273. mutexclpocall : [pocall_internproc];
  2274. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2275. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline]
  2276. ),(
  2277. idtok:_CPPDECL;
  2278. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2279. handler : nil;
  2280. pocall : pocall_cppdecl;
  2281. pooption : [];
  2282. mutexclpocall : [];
  2283. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2284. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2285. ),(
  2286. idtok:_VARARGS;
  2287. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2288. handler : nil;
  2289. pocall : pocall_none;
  2290. pooption : [po_varargs];
  2291. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2292. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2293. mutexclpotype : [];
  2294. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2295. ),(
  2296. idtok:_COMPILERPROC;
  2297. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2298. handler : nil;
  2299. pocall : pocall_none;
  2300. pooption : [po_compilerproc];
  2301. mutexclpocall : [];
  2302. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2303. mutexclpo : [po_interrupt]
  2304. ),(
  2305. idtok:_WEAKEXTERNAL;
  2306. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2307. handler : @pd_weakexternal;
  2308. pocall : pocall_none;
  2309. { mark it both external and weak external, so we don't have to
  2310. adapt all code for external symbols to also check for weak external
  2311. }
  2312. pooption : [po_external,po_weakexternal];
  2313. mutexclpocall : [pocall_internproc,pocall_syscall];
  2314. { allowed for external cpp classes }
  2315. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2316. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2317. ),(
  2318. idtok:_ENUMERATOR;
  2319. pd_flags : [pd_interface,pd_object,pd_record];
  2320. handler : @pd_enumerator;
  2321. pocall : pocall_none;
  2322. pooption : [];
  2323. mutexclpocall : [pocall_internproc];
  2324. mutexclpotype : [];
  2325. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2326. ),(
  2327. idtok:_RTLPROC;
  2328. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2329. handler : nil;
  2330. pocall : pocall_none;
  2331. pooption : [po_rtlproc];
  2332. mutexclpocall : [];
  2333. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2334. mutexclpo : [po_interrupt]
  2335. )
  2336. );
  2337. function check_proc_directive(isprocvar:boolean):boolean;
  2338. var
  2339. i : longint;
  2340. begin
  2341. result:=false;
  2342. for i:=1 to num_proc_directives do
  2343. if proc_direcdata[i].idtok=idtoken then
  2344. begin
  2345. if ((not isprocvar) or
  2346. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2347. { don't eat a public directive in classes }
  2348. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2349. result:=true;
  2350. exit;
  2351. end;
  2352. end;
  2353. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2354. {
  2355. Parse the procedure directive, returns true if a correct directive is found
  2356. }
  2357. var
  2358. p : longint;
  2359. found : boolean;
  2360. name : TIDString;
  2361. begin
  2362. parse_proc_direc:=false;
  2363. name:=tokeninfo^[idtoken].str;
  2364. found:=false;
  2365. { Hint directive? Then exit immediatly }
  2366. if (m_hintdirective in current_settings.modeswitches) then
  2367. begin
  2368. case idtoken of
  2369. _LIBRARY,
  2370. _PLATFORM,
  2371. _UNIMPLEMENTED,
  2372. _EXPERIMENTAL,
  2373. _DEPRECATED :
  2374. if (m_delphi in current_settings.modeswitches) and (pd.typ=procdef) then
  2375. begin
  2376. maybe_parse_hint_directives(tprocdef(pd));
  2377. { could the new token still be a directive? }
  2378. if token<>_ID then
  2379. exit;
  2380. name:=tokeninfo^[idtoken].str;
  2381. end
  2382. else
  2383. exit;
  2384. end;
  2385. end;
  2386. { C directive is MacPas only, because it breaks too much existing code
  2387. on other platforms (PFV) }
  2388. if (idtoken=_C) and
  2389. not(m_mac in current_settings.modeswitches) then
  2390. exit;
  2391. { retrieve data for directive if found }
  2392. for p:=1 to num_proc_directives do
  2393. if proc_direcdata[p].idtok=idtoken then
  2394. begin
  2395. found:=true;
  2396. break;
  2397. end;
  2398. { Check if the procedure directive is known }
  2399. if not found then
  2400. begin
  2401. { parsing a procvar type the name can be any
  2402. next variable !! }
  2403. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2404. not(idtoken=_PROPERTY) then
  2405. Message1(parser_w_unknown_proc_directive_ignored,name);
  2406. exit;
  2407. end;
  2408. { check if method and directive not for object, like public.
  2409. This needs to be checked also for procvars }
  2410. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2411. (symtablestack.top.symtabletype=ObjectSymtable) and
  2412. { directive allowed for cpp classes? }
  2413. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2414. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2415. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2416. exit;
  2417. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2418. (symtablestack.top.symtabletype=recordsymtable) then
  2419. exit;
  2420. { check if method and directive not for java class }
  2421. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2422. is_javaclass(tdef(symtablestack.top.defowner)) then
  2423. exit;
  2424. { check if method and directive not for java interface }
  2425. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2426. is_javainterface(tdef(symtablestack.top.defowner)) then
  2427. exit;
  2428. { Conflicts between directives ? }
  2429. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2430. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2431. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2432. begin
  2433. Message1(parser_e_proc_dir_conflict,name);
  2434. exit;
  2435. end;
  2436. { set calling convention }
  2437. if proc_direcdata[p].pocall<>pocall_none then
  2438. begin
  2439. if (po_hascallingconvention in pd.procoptions) then
  2440. begin
  2441. Message2(parser_w_proc_overriding_calling,
  2442. proccalloptionStr[pd.proccalloption],
  2443. proccalloptionStr[proc_direcdata[p].pocall]);
  2444. end;
  2445. { check if the target processor supports this calling convention }
  2446. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2447. begin
  2448. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2449. { recover }
  2450. proc_direcdata[p].pocall:=pocall_stdcall;
  2451. end;
  2452. pd.proccalloption:=proc_direcdata[p].pocall;
  2453. include(pd.procoptions,po_hascallingconvention);
  2454. end;
  2455. if pd.typ=procdef then
  2456. begin
  2457. { Check if the directive is only for objects }
  2458. if (pd_object in proc_direcdata[p].pd_flags) and
  2459. not assigned(tprocdef(pd).struct) then
  2460. exit;
  2461. { Check if the directive is only for records }
  2462. if (pd_record in proc_direcdata[p].pd_flags) and
  2463. not assigned(tprocdef(pd).struct) then
  2464. exit;
  2465. { check if method and directive not for interface }
  2466. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2467. is_interface(tprocdef(pd).struct) then
  2468. exit;
  2469. { check if method and directive not for interface }
  2470. if is_dispinterface(tprocdef(pd).struct) and
  2471. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2472. exit;
  2473. { check if method and directive not for objcclass }
  2474. if is_objcclass(tprocdef(pd).struct) and
  2475. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2476. exit;
  2477. { check if method and directive not for objcprotocol }
  2478. if is_objcprotocol(tprocdef(pd).struct) and
  2479. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2480. exit;
  2481. { check if method and directive not for record/class helper }
  2482. if is_objectpascal_helper(tprocdef(pd).struct) and
  2483. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2484. end;
  2485. { consume directive, and turn flag on }
  2486. consume(token);
  2487. parse_proc_direc:=true;
  2488. { Check the pd_flags if the directive should be allowed }
  2489. if (pd_interface in pdflags) and
  2490. not(pd_interface in proc_direcdata[p].pd_flags) then
  2491. begin
  2492. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2493. exit;
  2494. end;
  2495. if (pd_implemen in pdflags) and
  2496. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2497. begin
  2498. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2499. exit;
  2500. end;
  2501. if (pd_procvar in pdflags) and
  2502. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2503. begin
  2504. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2505. exit;
  2506. end;
  2507. { Return the new pd_flags }
  2508. if not(pd_body in proc_direcdata[p].pd_flags) then
  2509. exclude(pdflags,pd_body);
  2510. { Add the correct flag }
  2511. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2512. { Call the handler }
  2513. if pointer(proc_direcdata[p].handler)<>nil then
  2514. proc_direcdata[p].handler(pd);
  2515. end;
  2516. function proc_get_importname(pd:tprocdef):string;
  2517. var
  2518. dllname, importname : string;
  2519. begin
  2520. result:='';
  2521. if not(po_external in pd.procoptions) then
  2522. internalerror(200412151);
  2523. { external name or number is specified }
  2524. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2525. begin
  2526. if assigned(pd.import_dll) then
  2527. dllname:=pd.import_dll^
  2528. else
  2529. dllname:='';
  2530. if assigned(pd.import_name) then
  2531. importname:=pd.import_name^
  2532. else
  2533. importname:='';
  2534. proc_get_importname:=make_dllmangledname(dllname,
  2535. importname,pd.import_nr,pd.proccalloption);
  2536. end
  2537. else
  2538. begin
  2539. { Default names when importing variables }
  2540. case pd.proccalloption of
  2541. pocall_cdecl :
  2542. begin
  2543. if assigned(pd.struct) then
  2544. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2545. else
  2546. result:=target_info.Cprefix+pd.procsym.realname;
  2547. end;
  2548. pocall_cppdecl :
  2549. begin
  2550. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2551. end;
  2552. else
  2553. begin
  2554. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2555. { but according to MacPas mode description
  2556. Cprefix should still be used PM }
  2557. if (m_mac in current_settings.modeswitches) then
  2558. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2559. end;
  2560. end;
  2561. end;
  2562. end;
  2563. procedure proc_set_mangledname(pd:tprocdef);
  2564. var
  2565. s : string;
  2566. begin
  2567. { When the mangledname is already set we aren't allowed to change
  2568. it because it can already be used somewhere (PFV) }
  2569. if not(po_has_mangledname in pd.procoptions) then
  2570. begin
  2571. if (po_external in pd.procoptions) then
  2572. begin
  2573. { External Procedures are only allowed to change the mangledname
  2574. in their first declaration }
  2575. if (pd.forwarddef or (not pd.hasforward)) then
  2576. begin
  2577. s:=proc_get_importname(pd);
  2578. if s<>'' then
  2579. begin
  2580. pd.setmangledname(s);
  2581. end;
  2582. end;
  2583. end
  2584. else
  2585. { Normal procedures }
  2586. begin
  2587. if (po_compilerproc in pd.procoptions) then
  2588. begin
  2589. pd.setmangledname(lower(pd.procsym.name));
  2590. end;
  2591. end;
  2592. end;
  2593. { Public/exported alias names }
  2594. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2595. not(po_has_public_name in pd.procoptions) then
  2596. begin
  2597. case pd.proccalloption of
  2598. pocall_cdecl :
  2599. begin
  2600. if assigned(pd.struct) then
  2601. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2602. else
  2603. begin
  2604. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2605. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2606. (po_exports in pd.procoptions) then
  2607. pd.aliasnames.insert(pd.procsym.realname)
  2608. else
  2609. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2610. end;
  2611. end;
  2612. pocall_cppdecl :
  2613. begin
  2614. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2615. end;
  2616. end;
  2617. { prevent adding the alias a second time }
  2618. include(pd.procoptions,po_has_public_name);
  2619. end;
  2620. end;
  2621. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  2622. begin
  2623. if hcc_check in flags then
  2624. begin
  2625. { set the default calling convention if none provided }
  2626. if (pd.typ=procdef) and
  2627. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2628. is_cppclass(tprocdef(pd).struct)) then
  2629. begin
  2630. { none of the explicit calling conventions should be allowed }
  2631. if (po_hascallingconvention in pd.procoptions) then
  2632. internalerror(2009032501);
  2633. if is_cppclass(tprocdef(pd).struct) then
  2634. pd.proccalloption:=pocall_cppdecl
  2635. else
  2636. pd.proccalloption:=pocall_cdecl;
  2637. end
  2638. else if not(po_hascallingconvention in pd.procoptions) then
  2639. pd.proccalloption:=current_settings.defproccall
  2640. else
  2641. begin
  2642. if pd.proccalloption=pocall_none then
  2643. internalerror(200309081);
  2644. end;
  2645. { handle proccall specific settings }
  2646. case pd.proccalloption of
  2647. pocall_cdecl,
  2648. pocall_cppdecl :
  2649. begin
  2650. { check C cdecl para types }
  2651. check_c_para(pd);
  2652. end;
  2653. pocall_far16 :
  2654. begin
  2655. { Temporary stub, must be rewritten to support OS/2 far16 }
  2656. Message1(parser_w_proc_directive_ignored,'FAR16');
  2657. end;
  2658. end;
  2659. { Inlining is enabled and supported? }
  2660. if (po_inline in pd.procoptions) and
  2661. not(cs_do_inline in current_settings.localswitches) then
  2662. begin
  2663. { Give an error if inline is not supported by the compiler mode,
  2664. otherwise only give a hint that this procedure will not be inlined }
  2665. if not(m_default_inline in current_settings.modeswitches) then
  2666. Message(parser_e_proc_inline_not_supported)
  2667. else
  2668. Message(parser_h_inlining_disabled);
  2669. exclude(pd.procoptions,po_inline);
  2670. end;
  2671. { For varargs directive also cdecl and external must be defined }
  2672. if (po_varargs in pd.procoptions) then
  2673. begin
  2674. { check first for external in the interface, if available there
  2675. then the cdecl must also be there since there is no implementation
  2676. available to contain it }
  2677. if parse_only then
  2678. begin
  2679. { if external is available, then cdecl must also be available,
  2680. procvars don't need external }
  2681. if not((po_external in pd.procoptions) or
  2682. (pd.typ=procvardef) or
  2683. { for objcclasses this is checked later, because the entire
  2684. class may be external. }
  2685. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2686. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2687. Message(parser_e_varargs_need_cdecl_and_external);
  2688. end
  2689. else
  2690. begin
  2691. { both must be defined now }
  2692. if not((po_external in pd.procoptions) or
  2693. (pd.typ=procvardef)) or
  2694. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2695. Message(parser_e_varargs_need_cdecl_and_external);
  2696. end;
  2697. end;
  2698. end;
  2699. if hcc_insert_hidden_paras in flags then
  2700. begin
  2701. { insert hidden high parameters }
  2702. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2703. { insert hidden self parameter }
  2704. insert_self_and_vmt_para(pd);
  2705. { insert funcret parameter if required }
  2706. insert_funcret_para(pd);
  2707. { Make var parameters regable, this must be done after the calling
  2708. convention is set. }
  2709. { this must be done before parentfp is insert, because getting all cases
  2710. where parentfp must be in a memory location isn't catched properly so
  2711. we put parentfp never in a register }
  2712. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2713. { insert parentfp parameter if required }
  2714. insert_parentfp_para(pd);
  2715. end;
  2716. { Calculate parameter tlist }
  2717. pd.calcparas;
  2718. end;
  2719. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2720. {
  2721. Parse the procedure directives. It does not matter if procedure directives
  2722. are written using ;procdir; or ['procdir'] syntax.
  2723. }
  2724. var
  2725. res : boolean;
  2726. begin
  2727. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2728. begin
  2729. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2730. include(pd.procoptions,po_public);
  2731. include(pd.procoptions,po_has_public_name);
  2732. include(pd.procoptions,po_global);
  2733. end;
  2734. { methods from external class definitions are all external themselves }
  2735. if (pd.typ=procdef) and
  2736. assigned(tprocdef(pd).struct) and
  2737. (tprocdef(pd).struct.typ=objectdef) and
  2738. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  2739. tprocdef(pd).make_external;
  2740. { Class constructors and destructor are static class methods in real. }
  2741. { There are many places in the compiler where either class or static }
  2742. { method flag changes the behavior. It is simplier to add them to }
  2743. { the class constructors/destructors options than to fix all the }
  2744. { occurencies. (Paul) }
  2745. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2746. begin
  2747. include(pd.procoptions,po_classmethod);
  2748. include(pd.procoptions,po_staticmethod);
  2749. end;
  2750. while token in [_ID,_LECKKLAMMER] do
  2751. begin
  2752. if try_to_consume(_LECKKLAMMER) then
  2753. begin
  2754. repeat
  2755. parse_proc_direc(pd,pdflags);
  2756. until not try_to_consume(_COMMA);
  2757. consume(_RECKKLAMMER);
  2758. { we always expect at least '[];' }
  2759. res:=true;
  2760. end
  2761. else
  2762. begin
  2763. res:=parse_proc_direc(pd,pdflags);
  2764. end;
  2765. { A procedure directive normally followed by a semicolon, but in
  2766. a const section or reading a type we should stop when _EQ is found,
  2767. because a constant/default value follows }
  2768. if res then
  2769. begin
  2770. if (block_type=bt_const_type) and
  2771. (token=_EQ) then
  2772. break;
  2773. { support procedure proc;stdcall export; }
  2774. if not(check_proc_directive((pd.typ=procvardef))) then
  2775. begin
  2776. { support "record p : procedure stdcall end;" and
  2777. "var p : procedure stdcall = nil;" }
  2778. if (pd_procvar in pdflags) and
  2779. (token in [_END,_RKLAMMER,_EQ]) then
  2780. break
  2781. else
  2782. begin
  2783. if (token=_COLON) then
  2784. begin
  2785. Message(parser_e_field_not_allowed_here);
  2786. consume_all_until(_SEMICOLON);
  2787. end;
  2788. consume(_SEMICOLON)
  2789. end;
  2790. end;
  2791. end
  2792. else
  2793. break;
  2794. end;
  2795. { nostackframe requires assembler, but assembler
  2796. may be specified in the implementation part only,
  2797. and in not required if the function is first forward declared
  2798. if it is a procdef that has forwardef set to true
  2799. we postpone the possible error message to the real implementation
  2800. parse_only does not need to be considered as po_nostackframe
  2801. is an implementation only directive }
  2802. if (po_nostackframe in pd.procoptions) and
  2803. not (po_assembler in pd.procoptions) and
  2804. ((pd.typ<>procdef) or not tprocdef(pd).forwarddef) then
  2805. message(parser_e_nostackframe_without_assembler);
  2806. end;
  2807. procedure parse_var_proc_directives(sym:tsym);
  2808. var
  2809. pdflags : tpdflags;
  2810. pd : tabstractprocdef;
  2811. begin
  2812. pdflags:=[pd_procvar];
  2813. pd:=nil;
  2814. case sym.typ of
  2815. fieldvarsym,
  2816. staticvarsym,
  2817. localvarsym,
  2818. paravarsym :
  2819. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2820. typesym :
  2821. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2822. else
  2823. internalerror(2003042617);
  2824. end;
  2825. if pd.typ<>procvardef then
  2826. internalerror(2003042618);
  2827. { names should never be used anyway }
  2828. parse_proc_directives(pd,pdflags);
  2829. end;
  2830. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2831. var
  2832. pdflags : tpdflags;
  2833. begin
  2834. pdflags:=[pd_object];
  2835. parse_proc_directives(pd,pdflags);
  2836. end;
  2837. procedure parse_record_proc_directives(pd:tabstractprocdef);
  2838. var
  2839. pdflags : tpdflags;
  2840. begin
  2841. pdflags:=[pd_record];
  2842. parse_proc_directives(pd,pdflags);
  2843. end;
  2844. function proc_add_definition(var currpd:tprocdef):boolean;
  2845. {
  2846. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2847. forwarddef is found and reused it returns true
  2848. }
  2849. var
  2850. fwpd : tprocdef;
  2851. currparasym,
  2852. fwparasym : tsym;
  2853. currparacnt,
  2854. fwparacnt,
  2855. curridx,
  2856. fwidx,
  2857. i : longint;
  2858. po_comp : tprocoptions;
  2859. paracompopt: tcompare_paras_options;
  2860. forwardfound : boolean;
  2861. symentry: TSymEntry;
  2862. begin
  2863. forwardfound:=false;
  2864. { check overloaded functions if the same function already exists }
  2865. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2866. begin
  2867. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2868. { can happen for internally generated routines }
  2869. if (fwpd=currpd) then
  2870. begin
  2871. result:=true;
  2872. exit;
  2873. end;
  2874. { Skip overloaded definitions that are declared in other units }
  2875. if fwpd.procsym<>currpd.procsym then
  2876. continue;
  2877. { check the parameters, for delphi/tp it is possible to
  2878. leave the parameters away in the implementation (forwarddef=false).
  2879. But for an overload declared function this is not allowed }
  2880. if { check if empty implementation arguments match is allowed }
  2881. (
  2882. not(m_repeat_forward in current_settings.modeswitches) and
  2883. not(currpd.forwarddef) and
  2884. is_bareprocdef(currpd) and
  2885. not(po_overload in fwpd.procoptions)
  2886. ) or
  2887. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2888. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV)
  2889. don't check default values here, because routines that are the same except for their default
  2890. values should be reported as mismatches (since you can't overload based on different default
  2891. parameter values) }
  2892. (
  2893. (compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2894. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  2895. ) then
  2896. begin
  2897. { Check if we've found the forwarddef, if found then
  2898. we need to update the forward def with the current
  2899. implementation settings }
  2900. if fwpd.forwarddef then
  2901. begin
  2902. forwardfound:=true;
  2903. if not(m_repeat_forward in current_settings.modeswitches) and
  2904. (fwpd.proccalloption<>currpd.proccalloption) then
  2905. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2906. else
  2907. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2908. { Check calling convention }
  2909. if (fwpd.proccalloption<>currpd.proccalloption) then
  2910. begin
  2911. { In delphi it is possible to specify the calling
  2912. convention in the interface or implementation if
  2913. there was no convention specified in the other
  2914. part }
  2915. if (m_delphi in current_settings.modeswitches) then
  2916. begin
  2917. if not(po_hascallingconvention in currpd.procoptions) then
  2918. currpd.proccalloption:=fwpd.proccalloption
  2919. else
  2920. if not(po_hascallingconvention in fwpd.procoptions) then
  2921. fwpd.proccalloption:=currpd.proccalloption
  2922. else
  2923. begin
  2924. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2925. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2926. { restore interface settings }
  2927. currpd.proccalloption:=fwpd.proccalloption;
  2928. end;
  2929. end
  2930. else
  2931. begin
  2932. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2933. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2934. { restore interface settings }
  2935. currpd.proccalloption:=fwpd.proccalloption;
  2936. end;
  2937. end;
  2938. { Check static }
  2939. if (po_staticmethod in fwpd.procoptions) then
  2940. begin
  2941. if not (po_staticmethod in currpd.procoptions) then
  2942. begin
  2943. include(currpd.procoptions, po_staticmethod);
  2944. if (po_classmethod in currpd.procoptions) then
  2945. begin
  2946. { remove self from the hidden paras }
  2947. symentry:=currpd.parast.Find('self');
  2948. if symentry<>nil then
  2949. begin
  2950. currpd.parast.Delete(symentry);
  2951. currpd.calcparas;
  2952. end;
  2953. end;
  2954. end;
  2955. end;
  2956. { Check if the procedure type and return type are correct,
  2957. also the parameters must match also with the type and that
  2958. if the implementation has default parameters, the interface
  2959. also has them and that if they both have them, that they
  2960. have the same value }
  2961. if ((m_repeat_forward in current_settings.modeswitches) or
  2962. not is_bareprocdef(currpd)) and
  2963. ((compare_paras(fwpd.paras,currpd.paras,cp_all,paracompopt)<>te_exact) or
  2964. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)) then
  2965. begin
  2966. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2967. fwpd.fullprocname(false));
  2968. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2969. break;
  2970. end;
  2971. { Check if both are declared forward }
  2972. if fwpd.forwarddef and currpd.forwarddef then
  2973. begin
  2974. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2975. currpd.fullprocname(false));
  2976. end;
  2977. { internconst or internproc only need to be defined once }
  2978. if (fwpd.proccalloption=pocall_internproc) then
  2979. currpd.proccalloption:=fwpd.proccalloption
  2980. else
  2981. if (currpd.proccalloption=pocall_internproc) then
  2982. fwpd.proccalloption:=currpd.proccalloption;
  2983. { Check procedure options, Delphi requires that class is
  2984. repeated in the implementation for class methods }
  2985. if (m_fpc in current_settings.modeswitches) then
  2986. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2987. else
  2988. po_comp:=[po_classmethod,po_methodpointer];
  2989. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2990. (fwpd.proctypeoption <> currpd.proctypeoption) or
  2991. { if the implementation version has an "overload" modifier,
  2992. the interface version must also have it (otherwise we can
  2993. get annoying crashes due to interface crc changes) }
  2994. (not(po_overload in fwpd.procoptions) and
  2995. (po_overload in currpd.procoptions)) then
  2996. begin
  2997. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2998. fwpd.fullprocname(false));
  2999. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  3000. { This error is non-fatal, we can recover }
  3001. end;
  3002. { Forward declaration is external? }
  3003. if (po_external in fwpd.procoptions) then
  3004. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  3005. { Check parameters }
  3006. if (m_repeat_forward in current_settings.modeswitches) or
  3007. (currpd.minparacount>0) then
  3008. begin
  3009. { If mangled names are equal then they have the same amount of arguments }
  3010. { We can check the names of the arguments }
  3011. { both symtables are in the same order from left to right }
  3012. curridx:=0;
  3013. fwidx:=0;
  3014. currparacnt:=currpd.parast.SymList.Count;
  3015. fwparacnt:=fwpd.parast.SymList.Count;
  3016. repeat
  3017. { skip default parameter constsyms }
  3018. while (curridx<currparacnt) and
  3019. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  3020. inc(curridx);
  3021. while (fwidx<fwparacnt) and
  3022. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  3023. inc(fwidx);
  3024. { stop when one of the two lists is at the end }
  3025. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  3026. break;
  3027. { compare names of parameters, ignore implictly
  3028. renamed parameters }
  3029. currparasym:=tsym(currpd.parast.SymList[curridx]);
  3030. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  3031. if not(sp_implicitrename in currparasym.symoptions) and
  3032. not(sp_implicitrename in fwparasym.symoptions) then
  3033. begin
  3034. if (currparasym.name<>fwparasym.name) then
  3035. begin
  3036. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  3037. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  3038. break;
  3039. end;
  3040. end;
  3041. { next parameter }
  3042. inc(curridx);
  3043. inc(fwidx);
  3044. until false;
  3045. end;
  3046. { Everything is checked, now we can update the forward declaration
  3047. with the new data from the implementation }
  3048. fwpd.forwarddef:=currpd.forwarddef;
  3049. fwpd.hasforward:=true;
  3050. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  3051. { marked as local but exported from unit? }
  3052. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  3053. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  3054. if fwpd.extnumber=$ffff then
  3055. fwpd.extnumber:=currpd.extnumber;
  3056. while not currpd.aliasnames.empty do
  3057. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  3058. { update fileinfo so position references the implementation,
  3059. also update funcretsym if it is already generated }
  3060. fwpd.fileinfo:=currpd.fileinfo;
  3061. if assigned(fwpd.funcretsym) then
  3062. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  3063. if assigned(currpd.deprecatedmsg) then
  3064. begin
  3065. stringdispose(fwpd.deprecatedmsg);
  3066. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  3067. end;
  3068. { import names }
  3069. if assigned(currpd.import_dll) then
  3070. begin
  3071. stringdispose(fwpd.import_dll);
  3072. fwpd.import_dll:=stringdup(currpd.import_dll^);
  3073. end;
  3074. if assigned(currpd.import_name) then
  3075. begin
  3076. stringdispose(fwpd.import_name);
  3077. fwpd.import_name:=stringdup(currpd.import_name^);
  3078. end;
  3079. fwpd.import_nr:=currpd.import_nr;
  3080. { for compilerproc defines we need to rename and update the
  3081. symbolname to lowercase }
  3082. if (po_compilerproc in fwpd.procoptions) then
  3083. begin
  3084. { rename to lowercase so users can't access it }
  3085. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  3086. { the mangeled name is already changed by the pd_compilerproc }
  3087. { handler. It must be done immediately because if we have a }
  3088. { call to a compilerproc before it's implementation is }
  3089. { encountered, it must already use the new mangled name (JM) }
  3090. end;
  3091. { Release current procdef }
  3092. currpd.owner.deletedef(currpd);
  3093. currpd:=fwpd;
  3094. end
  3095. else
  3096. begin
  3097. { abstract methods aren't forward defined, but this }
  3098. { needs another error message }
  3099. if (po_abstractmethod in fwpd.procoptions) then
  3100. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  3101. else
  3102. begin
  3103. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  3104. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3105. end;
  3106. end;
  3107. { we found one proc with the same arguments, there are no others
  3108. so we can stop }
  3109. break;
  3110. end;
  3111. { check for allowing overload directive }
  3112. if not(m_fpc in current_settings.modeswitches) then
  3113. begin
  3114. { overload directive turns on overloading }
  3115. if ((po_overload in currpd.procoptions) or
  3116. (po_overload in fwpd.procoptions)) then
  3117. begin
  3118. { check if all procs have overloading, but not if the proc is a method or
  3119. already declared forward, then the check is already done }
  3120. if not(fwpd.hasforward or
  3121. assigned(currpd.struct) or
  3122. (currpd.forwarddef<>fwpd.forwarddef) or
  3123. ((po_overload in currpd.procoptions) and
  3124. (po_overload in fwpd.procoptions))) then
  3125. begin
  3126. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3127. break;
  3128. end
  3129. end
  3130. else
  3131. begin
  3132. if not(fwpd.forwarddef) then
  3133. begin
  3134. if (m_tp7 in current_settings.modeswitches) then
  3135. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  3136. else
  3137. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3138. break;
  3139. end;
  3140. end;
  3141. end; { equal arguments }
  3142. end;
  3143. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  3144. list }
  3145. if not forwardfound then
  3146. begin
  3147. { can happen in Delphi mode }
  3148. if (currpd.proctypeoption = potype_function) and
  3149. is_void(currpd.returndef) then
  3150. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3151. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3152. end;
  3153. proc_add_definition:=forwardfound;
  3154. end;
  3155. end.